-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFSExam.cpp
116 lines (112 loc) · 2.5 KB
/
BFSExam.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<bits/stdc++.h>
using namespace std;
class Graph
{
int V;
list<int>*adj;
public:
Graph(int V);
void addEdge(int u, int v);
void bfs(int source);
}
Graph::Graph(int V)
{
this->V = V;
adj = list<int>[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
}
void Graph::bfs(int source)
{
bool *visited = new bool[V];
for(int i=0; i<V; i++)
visited[i] = 0;
list<int>queue;
queue.push_back(source);
visited[source] = 1;
while(!queue.empty())
{
int x = queue.front();
queue.pop();
cout<<x;
list<int>::iterator it;
for(it = adj[x].begin(); it!= adj[x].end(); it++)
{
if(visited[*it] == 0)
{
visited[*it] = 1;
queue.push_back(*it);
}
}
}
}
void dfsUtil(int i, bool visited[])
{
visited[i] == true;
cout<<i;
list<int>::iterator it;
for(it = adj[i].begin(); it!=adj[i].end(); it++)
{
if(visited[*it] == false)
dfsUtil(*it, visited);
}
}
void dfs()
{
bool visited[] = new bool[V];
for(int i = 0; i<V; i++)
visited[i] = false;
for(int i=0; i<V; i++)
if(visited[i] == false)
dfsUtil(i, visited);
}
void dijkstra(list<pair<int,int>>*adj ,int s)
{
bool visited[V];
int distance[V];
for(int i=0; i<V; i++)
{
visited[i] = False;
distance[i] = INT32_MAX;
}
distance[s] = 0;
for(int i=0; i<V; i++)
{
int u = min(distance,visited);
visited[u] = true;
list<pair<int,int>>::iterator it;
for(it = adj[u].begin(); it!=adj[u].end(); it++)
{
if(distance[it->first] > distance[u]+it->second && distance[u]!=INT32_MAX && visited[it->first]==false)
distance[it->first] = distance[u] + it->second;
}
}
}
void topologicalSortUtil(int i, bool visited[], list<int>&stack)
{
visited[i] = true;
list<int>::iterator it;
for(it = adj[i].begin(); it!= adj[i].end(); it++)
if(!visited[*it])
topologicalSortUtil(*it, visited, stack);
stack.push(i);
}
void topologicalSort()
{
bool *visited = new bool[V];
list<int>stack[V];
for(int i=0; i<V; i++)
{
visited[i] = false;
}
for(int i=0; i<V; i++)
if(visited[i] == false)
topologicalSortUtil(i, visited, stack);
while(!stach.empty())
{
cout<<stack.top();
stack.pop();
}
}