-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfsattempt.cpp
101 lines (99 loc) · 2.29 KB
/
bfsattempt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include "QueueBFS.h"
#include "QueueBFS.cpp"
using namespace std;
struct Node
{
int value;
struct Node* next;
};
struct AdjList
{
struct Node* head;
};
struct Graph
{
int v;
bool visited[10000];
struct AdjList* arr;
Graph(int v)
{
this->v = v;
arr = new AdjList[v];
for(int i=0; i<v; i++)
{
arr[i].head = NULL;
}
}
Node* createNode(int val)
{
Node* newNode = new Node;
newNode->value = val;
newNode->next = NULL;
return newNode;
}
void addEdge(int src,int dest)
{
Node* newNode = createNode(dest);
newNode->next = arr[src].head;
arr[src].head = newNode;
// newNode = createNode(src);
// newNode->next = arr[dest].head;
//arr[dest].head = newNode;
}
void Printgraph()
{
for(int i=0; i<v; i++)
{
Node* newHead = arr[i].head;
cout<<"Children of: "<<i<<"are: ";
while(newHead)
{
cout<<newHead->value<<" ";
newHead = newHead->next;
}
cout<<endl;
}
}
void BFS(int node)
{
cout<<"BFS traverse \n";
Node* newNode = arr[node].head;
QueueBFS* newQueue = new QueueBFS(node);
while(true)
{
visited[node] = true;
cout<<newQueue->value<<endl;
while(newNode)
{
if(visited[newNode->value] ==false)
{
visited[newNode->value] = true;
newQueue->Insert(newNode->value);
}
newNode = newNode->next;
}
newQueue->Pop(&newQueue);
if(newQueue->is_Empty()) break;
newNode = arr[newQueue->value].head;
}
delete newQueue;
}
};
int main()
{
int n,m,a,b;
cout<<"Number of vertexes: ";
cin>>n;
Graph graph(n);
cout<<"Number of edges: ";
cin>>m;
for(int i=0;i<m;i++){
cout<<"Enter Edge num "<<i<<"| ";
cin>>a>>b;
graph.addEdge(a,b);
}
graph.Printgraph();
graph.BFS(0);
return 0;
}