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
91 changes: 91 additions & 0 deletions Graph/Basic/01 BFS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
// we are implementing tree with the help of queue
// for performing BFS we need a queue data structure // and this queue is using linkedlist
struct Node
{
int data;
struct Node* next;
}*front = NULL, * rear = NULL; //front and rear pointer to help in insertion and deletion of nodes in Queue

void enqueue(int x)
{
struct Node* t;
t = (struct Node*)malloc(sizeof(struct Node)); //Node is created in Heap

if (t == NULL) //if node was not created due to insufficient space in the HEAP
printf("Queue is Full\n");
else
{
t->data = x;
t->next = NULL;

if (front == NULL) //if front is NULL it means it is the first node of the queue //first node condition
front = rear = t; //if it is the first node make front and rear both point on the first node
else //if it is not the first node
{
rear->next = t; //last node which is rear //rear->next points on the new node
rear = t; //the new node is updated as the rear
}
}
}

int dequeue()
{
int x = -1; //If Queue is empty the function will return -1
struct Node* t; //temporary pointer to hold the address of the node that is to be deleted

if (front == NULL) //If front is NULL there is no nodes in the Queue
printf("Queue is Empty\n");
else
{
t = front; //temp pointer is made to point on the front node
front = front->next; //front node is updated to its next node
x = t->data; //x assigns the data value of t node
free(t); //t node is deleted
}
return x; //x is returned as the deleted element
}
int isEmpty()
{
return front == NULL;
}
void BFS(int G[][7], int start, int n) // it sud take matrix i.e G 1st dimension is not require // starting point // and the order of the matrix i.e 7x7
{
int i = start, j; // for start of a ttaversal we need a variable
int visited[7] = { 0 }; // we need an array i.e visited array size of 7 intialized with zero

printf("%d ", i); //1st of all print the starting vertex taked vertex in i
visited[i] = 1; // mark the starting vertex as visited
enqueue(i); // then push i in the queue
// then i sud perform a repeting task
while (!isEmpty()) // while there is vertex
{
i = dequeue(); // everytime i will get a vertex
for (j = 1; j < n; j++) // as long as j is less then n i.e 7
{
if (G[i][j] == 1 && visited[j] == 0) // everytime check either edge or vertex is visited or not ==1 means there is an edge // and it is not yet visited
{
printf("%d ", j); // print j
visited[j] = 1; // mark visited of j as 1
enqueue(j); // push j in queue
}
}
}


}

int main()
{
int G[7][7] = { {0,0,0,0,0,0,0}, // we are not using 0th row and 0th column // they all have value zero
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0} };
BFS(G, 4, 7); // matrix // starting // order

return 0;
}
108 changes: 108 additions & 0 deletions Graph/Basic/02 DFS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdio.h>
#include <stdlib.h>
// we are implementing tree with the help of queue
// for performing BFS we need a queue data structure // and this queue is using linkedlist
struct Node
{
int data;
struct Node* next;
}*front = NULL, * rear = NULL; //front and rear pointer to help in insertion and deletion of nodes in Queue

void enqueue(int x)
{
struct Node* t;
t = (struct Node*)malloc(sizeof(struct Node)); //Node is created in Heap

if (t == NULL) //if node was not created due to insufficient space in the HEAP
printf("Queue is Full\n");
else
{
t->data = x;
t->next = NULL;

if (front == NULL) //if front is NULL it means it is the first node of the queue //first node condition
front = rear = t; //if it is the first node make front and rear both point on the first node
else //if it is not the first node
{
rear->next = t; //last node which is rear //rear->next points on the new node
rear = t; //the new node is updated as the rear
}
}
}

int dequeue()
{
int x = -1; //If Queue is empty the function will return -1
struct Node* t; //temporary pointer to hold the address of the node that is to be deleted

if (front == NULL) //If front is NULL there is no nodes in the Queue
printf("Queue is Empty\n");
else
{
t = front; //temp pointer is made to point on the front node
front = front->next; //front node is updated to its next node
x = t->data; //x assigns the data value of t node
free(t); //t node is deleted
}
return x; //x is returned as the deleted element
}
int isEmpty()
{
return front == NULL;
}
void BFS(int G[][7], int start, int n) // it sud take matrix i.e G 1st dimension is not require // starting point // and the order of the matrix i.e 7x7
{
int i = start, j; // for start of a ttaversal we need a variable
int visited[7] = { 0 }; // we need an array i.e visited array size of 7 intialized with zero

printf("%d ", i); //1st of all print the starting vertex taked vertex in i
visited[i] = 1; // mark the starting vertex as visited
enqueue(i); // then push i in the queue
// then i sud perform a repeting task
while (!isEmpty()) // while there is vertex
{
i = dequeue(); // everytime i will get a vertex
for (j = 1; j < n; j++) // as long as j is less then n i.e 7
{
if (G[i][j] == 1 && visited[j] == 0) // everytime check either edge or vertex is visited or not ==1 means there is an edge // and it is not yet visited
{
printf("%d ", j); // print j
visited[j] = 1; // mark visited of j as 1
enqueue(j); // push j in queue
}
}
}


}
void DFS(int G[][7], int start, int n)// it sud take matrix i.e G 1st dimension is not require // starting point // and the order of the matrix i.e 7x7
{
static int visited[7] = { 0 }; // declear it as static array so that it can be accsessible to every call // intialized it as zero
int j;

if (visited[start] == 0) // if it is not yet visited
{
printf("%d ", start); // print that vertex
visited[start] = 1; // mark it as visited else it will go infite loop

for (j = 1; j < n; j++) // we have to explore vertes so go through loop
{
if (G[start][j] == 1 && visited[j] == 0) // everytime we check that if it is an edge from i i.e starting vertex // if it is not yet visited
DFS(G, j, n); // recursion call
}
}
}
int main()
{
int G[7][7] = { {0,0,0,0,0,0,0}, // 1st row and 1st col is zero
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0} };
DFS(G, 3, 7); // graph // staring index // order of matrix

return 0;
}

94 changes: 94 additions & 0 deletions Graph/Basic/03 PrimMinSpanningTreeFunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#define V 8
#define I 32767

using namespace std;

void PrintMST(int T[][V-2], int G[V][V]){
cout << "\nMinimum Spanning Tree Edges (w/ cost)\n" << endl;
int sum {0};
for (int i {0}; i<V-2; i++){
int c = G[T[0][i]][T[1][i]];
cout << "[" << T[0][i] << "]---[" << T[1][i] << "] cost: " << c << endl;
sum += c;
}
cout << endl;
cout << "Total cost of MST: " << sum << endl;
}

void PrimsMST(int G[V][V], int n){
int u;
int v;
int min {I};
int track [V];
int T[2][V-2] {0};

// Initial: Find min cost edge
for (int i {1}; i<V; i++){
track[i] = I; // Initialize track array with INFINITY
for (int j {i}; j<V; j++){
if (G[i][j] < min){
min = G[i][j];
u = i;
v = j;
}
}
}
T[0][0] = u;
T[1][0] = v;
track[u] = track[v] = 0;

// Initialize track array to track min cost edges
for (int i {1}; i<V; i++){
if (track[i] != 0){
if (G[i][u] < G[i][v]){
track[i] = u;
} else {
track[i] = v;
}
}
}

// Repeat
for (int i {1}; i<n-1; i++){
int k;
min = I;
for (int j {1}; j<V; j++){
if (track[j] != 0 && G[j][track[j]] < min){
k = j;
min = G[j][track[j]];
}
}
T[0][i] = k;
T[1][i] = track[k];
track[k] = 0;

// Update track array to track min cost edges
for (int j {1}; j<V; j++){
if (track[j] != 0 && G[j][k] < G[j][track[j]]){
track[j] = k;
}
}
}
PrintMST(T, G);
}

int main() {

int cost [V][V] {
{I, I, I, I, I, I, I, I},
{I, I, 25, I, I, I, 5, I},
{I, 25, I, 12, I, I, I, 10},
{I, I, 12, I, 8, I, I, I},
{I, I, I, 8, I, 16, I, 14},
{I, I, I, I, 16, I, 20, 18},
{I, 5, I, I, I, 20, I, I},
{I, I, 10, I, 14, 18, I, I},
};

int n = sizeof(cost[0])/sizeof(cost[0][0]) - 1;

PrimsMST(cost, n);

return 0;
}
89 changes: 89 additions & 0 deletions Graph/Basic/04 KrushalsMinCostSpanningTreeFunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>

#define I 32767 // Infinity
#define V 7 // # of vertices in Graph
#define E 9 // # of edges in Graph

using namespace std;

void PrintMCST(int T[][V-1], int A[][E]){
cout << "\nMinimum Cost Spanning Tree Edges\n" << endl;
for (int i {0}; i<V-1; i++){
cout << "[" << T[0][i] << "]-----[" << T[1][i] << "]" << endl;
}
cout << endl;
}

// Set operations: Union and Find
void Union(int u, int v, int s[]){
if (s[u] < s[v]){
s[u] += s[v];
s[v] = u;
} else {
s[v] += s[u];
s[u] = v;
}
}

int Find(int u, int s[]){
int x = u;
int v = 0;

while (s[x] > 0){
x = s[x];
}

while (u != x){
v = s[u];
s[u] = x;
u = v;
}
return x;
}

void KruskalsMCST(int A[3][9]){
int T[2][V-1]; // Solution array
int track[E] {0}; // Track edges that are included in solution
int set[V+1] = {-1, -1, -1, -1, -1, -1, -1, -1}; // Array for finding cycle

int i {0};
while (i < V-1){
int min = I;
int u {0};
int v {0};
int k {0};

// Find a minimum cost edge
for (int j {0}; j<E; j++){
if (track[j] == 0 && A[2][j] < min){
min = A[2][j];
u = A[0][j];
v = A[1][j];
k = j;
}
}

// Check if the selected min cost edge (u, v) forming a cycle or not
if (Find(u, set) != Find(v, set)){
T[0][i] = u;
T[1][i] = v;

// Perform union
Union(Find(u, set), Find(v, set), set);
i++;
}
track[k] = 1;
}

PrintMCST(T, A);
}

int main() {
int edges[3][9] = {{ 1, 1, 2, 2, 3, 4, 4, 5, 5},
{ 2, 6, 3, 7, 4, 5, 7, 6, 7},
{25, 5, 12, 10, 8, 16, 14, 20, 18}};

KruskalsMCST(edges);

return 0;
}
Loading