Skip to content

Commit f4f50a2

Browse files
authored
Merge pull request larissalages#164 from vaishnavirshah/master
Added graph implementation in CPP along with DFS and BFS codes
2 parents aee520d + fb82519 commit f4f50a2

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
using namespace std;
4+
int n =5;
5+
typedef struct node{
6+
int data;
7+
struct node *next;
8+
}node;
9+
10+
11+
void sortedInsert(node **p, int v){
12+
node *newnode;
13+
newnode = (node *) malloc (sizeof(node));
14+
newnode->data = v;
15+
newnode->next=0;
16+
17+
if(*p == 0 || (*p)->data > v){
18+
newnode->next = (*p);
19+
*p =newnode;
20+
}
21+
else{
22+
node *temp;
23+
temp=*p;
24+
while (temp->next != 0 && temp->next->data < v) {
25+
temp = temp->next;
26+
}
27+
newnode->next = temp->next;
28+
temp->next = newnode;
29+
}
30+
31+
}
32+
void display(node *p){
33+
while(p!=0){
34+
cout<<p->data<<" ";
35+
p=p->next;
36+
}
37+
cout<<endl;
38+
}
39+
40+
void BSFll(node* a[], int visit[], int row=0){
41+
int queue[n];
42+
int top =-1, i, bottom =-1;
43+
visit[row]=1;
44+
queue[++bottom] = row;
45+
cout<<(char)(row+65)<<" ";
46+
while(top != bottom){
47+
row= queue[++top];
48+
node * temp;
49+
temp = a[row];
50+
while(temp != NULL){
51+
i = temp->data;
52+
if(!visit[i]){
53+
visit[i]=1;
54+
cout<<(char)(i+65)<<" ";
55+
queue[++bottom] =i;
56+
}
57+
temp = temp->next;
58+
}
59+
}
60+
}
61+
void DSFll(node* a[], int visit[], int row=0){
62+
int stack[n];
63+
int top = -1,i;
64+
visit[row]=1;
65+
stack[++top]= row;
66+
cout<<(char)(row+65)<<" ";
67+
while(top!= -1){
68+
row= stack[top];
69+
node * temp;
70+
temp = a[row];
71+
while(temp != NULL){
72+
i = temp->data;
73+
if(!visit[i]){
74+
visit[i]=1;
75+
cout<<(char)(i+65)<<" ";
76+
stack[++top]=i;
77+
break;
78+
}
79+
temp = temp->next;
80+
}
81+
if(temp == NULL){
82+
top--;
83+
}
84+
}
85+
}
86+
87+
88+
void createGraph(node *a[], int s, int d){
89+
sortedInsert(&a[s], d);
90+
// condition for directed
91+
sortedInsert(&a[d], s);
92+
}
93+
int main()
94+
{
95+
node* a[5]= {NULL};
96+
node *first =0, *second=0, *third=0, *fourth=0, *fifth=0;
97+
a[2] = third;
98+
a[1] = second;
99+
a[0] = first;
100+
a[3] = fourth;
101+
a[4] = fifth;
102+
int visit[5] ={0};
103+
createGraph(a, 0, 2);
104+
createGraph(a,1,2);
105+
createGraph(a,2,3);
106+
createGraph(a,3,4);
107+
BSFll(a,visit);
108+
return 0;
109+
}
110+
//Contributed by Vaishnavi Shah
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <cstring>
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
int n=5;
7+
void DFS(int matrix[5][5], int visit[],int visitTo=0){
8+
cout<<(char)(visitTo+65)<<" ";
9+
visit[visitTo]=1;
10+
for(int i=0; i<5; i++){
11+
if(matrix[visitTo][i] && !visit[i])
12+
DFS(matrix,visit,i);
13+
}
14+
}
15+
void BFS(int matrix[5][5], int visit[], int row=0){
16+
int queue[n];
17+
int top =-1, i, bottom =-1;
18+
visit[row]=1;
19+
queue[++bottom] = row;
20+
cout<<(char)(row+65)<<" ";
21+
while(top != bottom){
22+
row= queue[++top];
23+
for(i=0; i<n; i++){
24+
if(matrix[row][i] && !visit[i]){
25+
visit[i]=1;
26+
cout<<row<<" "<<(char)(i+65)<<" ";
27+
queue[++bottom] =i;
28+
}
29+
}
30+
}
31+
}
32+
int main()
33+
{
34+
int matrix[5][5]={ {0,0,3,0,4}, {0,0,6,0,1},{3,6,0,5,0},{0,0,5,0,7},{4,1,0,7,0} };
35+
int visit[5]={0};
36+
DFS(matrix,visit);
37+
return 0;
38+
}
39+
//Contributed by Vaishnavi Shah

0 commit comments

Comments
 (0)