File tree Expand file tree Collapse file tree 4 files changed +119
-0
lines changed
Competitive Coding/Graphs/Graph_Search Expand file tree Collapse file tree 4 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1+ This is the algortihm for Breadth First Search in a given graph.
2+
3+ -We take a starting vertex , and push all its adjacent vertexes in a queue.
4+ -Now we pop an element from a queue and push all its vertexes in the queue , and we also mark down these vertexes as visited.
5+ -After executing the code, we will get the vertex visited in a breadth first manner.
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ vector<bool >Visited;
5+ vector< vector<int > >Graph;
6+
7+ void edge (int a,int b)
8+ {
9+ Graph[a].push_back (b); // for directed
10+
11+ // If undirected graph is there, then add the following line
12+ // Graph[b].push_back(a);
13+ }
14+
15+ void BreadthFirstSearch (int u)
16+ {
17+ queue<int >q;
18+ q.push (u);
19+ Visited[u]=true ;
20+
21+ while (!q.empty ())
22+ {
23+ int f=q.front ();
24+ q.pop ();
25+
26+ cout<<f<<" " ;
27+ int ln=Graph[f].size ();
28+ for (int j=0 ;j<ln;j++)
29+ {
30+ if (!Visited[Graph[f][j]])
31+ {
32+ q.push (Graph[f][j]);
33+ Visited[Graph[f][j]]=true ;
34+ }
35+ }
36+ }
37+ }
38+
39+ int main ()
40+ {
41+ int n,e;
42+ cin>>n>>e;
43+ Visited.assign (n,false );
44+ Graph.assign (n,vector<int >());
45+
46+ int a,b;
47+ for (int i=0 ;i<e;i++)
48+ {
49+ cin>>a>>b;
50+ edge (a,b);
51+ }
52+
53+ for (int i=0 ;i<n;i++)
54+ {
55+ if (!Visited[i])
56+ {
57+ BreadthFirstSearch (i);
58+ }
59+ }
60+ return 0 ;
61+ }
Original file line number Diff line number Diff line change 1+ This is the algortihm for Depth First Search in a given graph.
2+
3+ -We take a starting vertex , and push all its adjacent vertexes in a stack.
4+ -Now we pop an element from a stack and push all its vertexes in the stack , and we also mark down these vertexes as visited.
5+ -After executing the code, we will get the vertex visited in a depth first manner.
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ void addEdge (vector<int >adj[],int u,int v)
5+ {
6+ // For undirected graph
7+ adj[u].push_back (v);
8+ adj[v].push_back (u);
9+ }
10+
11+ void DFSUtil (int u,vector<int >adj[],vector<bool >&visited)
12+ {
13+ visited[u]=true ;
14+ cout<<u<<" " ;
15+ for (int i=0 ;i<adj[u].size ();i++)
16+ {
17+ if (visited[adj[u][i]]==false )
18+ DFSUtil (adj[u][i],adj,visited);
19+ }
20+ }
21+
22+ void DFS (vector<int >adj[],int V)
23+ {
24+ vector<bool >visited (V,false );
25+ for (int u=0 ;u<V;u++)
26+ {
27+ if (visited[u]==false )
28+ {
29+ DFSUtil (u,adj,visited);
30+ }
31+ }
32+ }
33+
34+ int main ()
35+ {
36+ int V = 5 ;
37+ vector<int > adj[V];
38+ addEdge (adj, 0 , 1 );
39+ addEdge (adj, 0 , 4 );
40+ addEdge (adj, 1 , 2 );
41+ addEdge (adj, 1 , 3 );
42+ addEdge (adj, 1 , 4 );
43+ addEdge (adj, 2 , 3 );
44+ addEdge (adj, 3 , 4 );
45+ DFS (adj, V);
46+ cout<<" \n " ;
47+ return 0 ;
48+ }
You can’t perform that action at this time.
0 commit comments