Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dsa1/assignment6/Matrix_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
4
3
0 0 0
1 0 2
0 6 1
7 0 0
6 changes: 6 additions & 0 deletions dsa1/assignment6/Matrix_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
4
3
0 2 0
-1 0 0
0 -2 0
0 0 4
4 changes: 2 additions & 2 deletions dsa1/assignment6/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ int main(){
writeIntoFile("matrix4.txt", *s4);
*/

readFromFile("Matrix1.txt", &s1);
readFromFile("Matrix_1.txt", &s1);
display(s1);

readFromFile("Matrix2.txt", &s2);
readFromFile("Matrix_2.txt", &s2);
display(s2);

Sparse * s3 = add(s1, s2);
Expand Down
150 changes: 82 additions & 68 deletions dsa2/assignment5/logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
#include <limits.h>
#include "header.h"

//---------------Utility function---------------

void swap(int *a, int *b)
{
int temp = *a;

*a = *b;
*b = temp;
return;
Comment thread
krishna-warfade marked this conversation as resolved.
}

//---------------Max Heap Operations---------------

int isEmptyMax(MaxHeap h)
{
return (h.rear == -1);
Expand All @@ -19,16 +24,6 @@ int isFullMax(MaxHeap h)
{
return (h.rear == h.size - 1);
}

int isEmptyMin(MinHeap h)
Comment thread
krishna-warfade marked this conversation as resolved.
{
return (h.rear == -1);
}
int isFullMin(MinHeap h)
{
return (h.rear == h.size - 1);
}

void initMax(MaxHeap *h, int size)
{
h->A = (int *)malloc(sizeof(int) * size);
Expand All @@ -37,27 +32,8 @@ void initMax(MaxHeap *h, int size)
return;
}

void MaxHeapSort(int *arr, int size)
Comment thread
krishna-warfade marked this conversation as resolved.
{
MaxHeap h;
initMax(&h, size);

for (int i = 0; i < size; i++)
{
insetMax(&h, arr[i]);
}

int i = 0;
while (!isEmptyMax(h))
{
arr[i] = removeMax(&h);
i++;
}

return;
}

void insetMax(MaxHeap *h, int value)
// Insert Element into Max Heap
void insertMax(MaxHeap *h, int value)
{
if (isFullMax(*h))
return;
Expand All @@ -76,19 +52,7 @@ void insetMax(MaxHeap *h, int value)
return;
}

int removeMax(MaxHeap *h)
{
if (isEmptyMax(*h))
return INT_MIN;

int removedElement = h->A[0];
h->A[0] = h->A[h->rear];
h->rear--;
maxHeapify(h, 0);

return removedElement;
}

// Creating Max Heap
void maxHeapify(MaxHeap *h, int index)
{
int largest = index;
Expand All @@ -110,35 +74,61 @@ void maxHeapify(MaxHeap *h, int index)
return;
}

void initMin(MinHeap *h, int size)
// Remove Max Element from Heap
int removeMax(MaxHeap *h)
{
h->A = (int *)malloc(sizeof(int) * size);
h->size = size;
h->rear = -1;
return;
if (isEmptyMax(*h))
return INT_MIN;

int removedElement = h->A[0];
h->A[0] = h->A[h->rear];
h->rear--;
maxHeapify(h, 0);

return removedElement;
}

void MinHeapSort(int *arr, int size)
// Heap Sort using Max Heap
void MaxHeapSort(int *arr, int size)
{
MinHeap h;
initMin(&h, size);
MaxHeap h;
initMax(&h, size);

for (int i = 0; i < size; i++)
{
insetMin(&h, arr[i]);
insertMax(&h, arr[i]);
}

int i = 0;
while (!isEmptyMin(h))
while (!isEmptyMax(h))
{
arr[i] = removeMin(&h);
arr[i] = removeMax(&h);
i++;
}

return;
}

void insetMin(MinHeap *h, int value)
//---------------Min Heap Operations---------------

int isEmptyMin(MinHeap h)
{
return (h.rear == -1);
}
int isFullMin(MinHeap h)
{
return (h.rear == h.size - 1);
}
void initMin(MinHeap *h, int size)
{
h->A = (int *)malloc(sizeof(int) * size);
h->size = size;
h->rear = -1;
return;
}

// Insert element into Min Heap
void insertMin(MinHeap *h, int value)
{
if (isFullMin(*h))
return;
Expand All @@ -157,6 +147,29 @@ void insetMin(MinHeap *h, int value)
return;
}

// Creating Min Heap
void minHeapify(MinHeap *h, int index)
{
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < h->rear && h->A[left] < h->A[smallest])
smallest = left;

if (right < h->rear && h->A[right] < h->A[smallest])
smallest = right;

if (smallest != index)
{
swap(&h->A[index], &h->A[smallest]);
minHeapify(h, smallest);
}

return;
}

// Remove Min Element from Heap
int removeMin(MinHeap *h)
{
if (isEmptyMin(*h))
Expand All @@ -170,22 +183,23 @@ int removeMin(MinHeap *h)
return removedElement;
}

void minHeapify(MinHeap *h, int index)
{
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < h->rear && h->A[left] < h->A[smallest])
smallest = left;
// Heap Sort using Min Heap
void MinHeapSort(int *arr, int size)
{
MinHeap h;
initMin(&h, size);

if (right < h->rear && h->A[right] < h->A[smallest])
smallest = right;
for (int i = 0; i < size; i++)
{
insertMin(&h, arr[i]);
}

if (smallest != index)
int i = 0;
while (!isEmptyMin(h))
{
swap(&h->A[index], &h->A[smallest]);
minHeapify(h, smallest);
arr[i] = removeMin(&h);
i++;
}

return;
Expand Down
5 changes: 5 additions & 0 deletions graph/graphADT/graph.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4
0 2 0 0
2 0 7 9
0 7 0 1
0 9 1 0
17 changes: 17 additions & 0 deletions graph/graphADT/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>

typedef struct graph
{
int ** G;
int vertices;
int edges;
} Graph;

void init(Graph *g, int vertices);
void addEdge(Graph *g, int src, int des, int weight);
void createGraph(Graph *g, FILE *f);
void display(Graph g);
int isAdjacent(Graph g, int src, int des);
int getDegree(Graph g, int vertex);
int isConnected(Graph g);
void displayComponents(Graph g);
Loading