International Islamic University H-10, Islamabad, Pakistan
Data Structures
Lecture No. 21
Graph, DFS, BFS
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
 Graphs are composed of Nodes (vertices) and Edges.
 A graph is a set of vertices (V) with links or edges (E) connecting them
 Linked lists, trees, and heaps are all special cases of graphs
 Notation: A graph G is a pair (V, E) i.e G = (V, E)
 Each edge is a pair (v1, v2), where v1, v2 are vertices in V
 In a directed graph (digraph), all edges have directions
 In an undirected graph, an edge does not have a direction
 For Example
G = (V, E)
where V={1, 2, 3, 4, 5, 6}
E={(1, 2), (1, 6), (2, 3), (3, 4), (4, 5), (4, 6), (5, 6)}
Graphs
2
5
6
1 3
4
Node
or
Vertex
Edge
 If the order of edge pairs (v1, v2) matters then the graph is directed (also called
a digraph): (v1, v2) ≠ (v2, v1)
 If the order of edge pairs (v1, v2) does not matter, the graph is called an
undirected graph: in this case, (v1 , v2) = (v2 , v1)
 Vertex v1 is adjacent to vertex v2, if there is an edge (v1, v2)
 In a digraph, existence of edge (v1, v2) does not mean v1 is adjacent to v2
 In an undirected graph, existence of edge (v1, v2) means both v1 and v2 are
adjacent to each other.
 An edge may have a weight or cost
Directed vs Undirected Graphs
v1 v2
A Directed Graph Undirected Graph v1 v2
v1 v2
15
- 4
 Path: a sequence of vertices v1, v2 …, vn such that consecutive vertices vj , vj+1
have an edge between them.
 Length: length of a path is the number of edges in the path
 Loop: an edge from a vertex onto itself, denoted by (v, v).
Definitions
v5
v4
v3
v1
v2
Simple Path = v1, v2, v3 v4, v5
Length = 5
v6
Loop (v6, v6)
 Simple Path: a path in a graph is simple if all vertices are distinct
 v1 , v3 , v4 (a simple path)
 v1 , v3 , v4 , v6 , v3 (a path but not a simple path)
 Cycle: a cycle is a path of length at least 1 such that the first and the last
vertices are the same.
 v1 , v2 , v3 , v1 (A Simple Cycle)
 v1 , v2 , v3 , v1 , v1 , v3 (A Cycle but not Simple Cycle)
Definitions
v5
v4
v3
v1
v2 v6
 A undirected graph is connected if there is a path from every vertex to every
other vertex. A digraph with this property is strongly connected
 If a directed graph is not strongly connected, but underlying undirected graph
is connected then the directed graph is weakly connected
Definitions
v1 v3
v4
v2
strongly connected
v1 v3
v4
v2
weakly connected
v1 v3
v4
v2
disconnected
Graph Applications
Course Prerequisites
Nodes = courses
Directed edge = prerequisite
Representing a Maze
Nodes = rooms
Edge = door or passage
Representing Electrical Circuits
Computer Networks
Nodes = battery, switch, resistor, etc.
Edges = connections
Nodes = computers
Edges = transmission rates
 Driving Map
 Edge = Road
 Vertex = Intersection
 Edge weight = Time required to cover
the road
 Airline Traffic
 Vertex = Cities serviced by the airline
 Edge = Flight exists between two cities
 Edge weight = Flight time or flight cost
 Computer networks
 Vertex = Switches, routers
 Edge = Communication links
 Edge weight = Delay, hop count, cost
Graph Applications
 Clash Free Datasheet
 Edge = Paper
 Vertex = Date
 Google Maps
 Edge = Roads
 Vertex = Location
 Dijkstra's Algorithm
 Prims's Algorithm
 Kruskal's Algorithm
 There are at least two ways of representing graphs:
 The adjacency matrix representation
 The adjacency list representation
 An adjacency matrix for a graph with n vertices numbered
0,1,...,n-1 is an n by n array matrix such that matrix[i][j] is 1
(true) if there is an edge from vertex i to vertex j and 0
(false) otherwise.
 When the graph is weighted, we can let matrix[i][j] be the
weight that labels the edge from vertex i to vertex j
instead of simply 1, and let matrix[i][j] equal to ∞ instead
of 0 when there is no edge from vertex i to vertex j
 Adjacency matrix for an undirected graph is symmetrical.
 i.e. matrix[i][j] is equal to matrix[j][i]
Graph Implementation
A B
D
C
A Graph
A B C D
A 0 0 1 1
B 0 0 0 0
C 1 0 0 1
D 1 0 1 0
Adjacency Matrix
 An adjacency list for a graph with n vertices numbered 0,1,... ,n-
1 consists of n linked lists.
 The ith linked list has a node for vertex j if and only if the graph
contains an edge from vertex i to vertex j.
 Adjacency list is a better solution if the graph is sparse.
 Space requirement is O(|E| + |V|), which is linear in the size of
the graph. In an undirected graph each edge (v,w) appears in
two lists. So space requirement is doubled.
Adjacency List
A B
D
C
Undirected Weighted Graph
A B C D
A ∞ ∞ 6 9
B ∞ ∞ ∞ ∞
C 6 ∞ ∞ 8
D 9 ∞ 8 ∞
Its Adjacency Matrix
6
8
9
0 A
1 B
2 C
3 D
NULL
Adjacency List
C 6
A 6
A 9
D 9
D 8
NULL
NULL
C 8 NULL
Adjacency Matrix for a Digraph
A B
D
C
A Diagraph
A B C D
A 0 0 1 1
B 0 0 0 0
C 0 0 0 1
D 0 0 0 0
Adjacency Matrix
0 A
1 B
2 C
3 D
NULL
Adjacency List
C
D
D NULL
NULL
NULL
 Two common graph operations:
1. Determine whether there is an edge from vertex i to vertex j.
2. Find all vertices adjacent to a given vertex i.
 An adjacency matrix supports operation 1 more efficiently.
 An adjacency list supports operation 2 more efficiently.
 An adjacency list often requires less space than an adjacency matrix.
 Adjacency Matrix: Space requirement is O(|V|2)
 Adjacency List: Space requirement is O(|E| + |V|), which is linear in the size
of the graph.
 Adjacency matrix is better if the graph is dense (too many edges)
 Adjacency list is better if the graph is sparse (few edges)
Adjacency Matrix vs Adjacency List
 A graph-traversal algorithm starts from a vertex v, visits all of the vertices that
can be reachable from the vertex v.
 A graph-traversal algorithm visits all vertices if and only if the graph is
connected.
 A connected component is the subset of vertices visited during a traversal
algorithm that begins at a given vertex.
 A graph-traversal algorithm must mark each vertex during a visit and must
never visit a vertex more than once.
 Thus, if a graph contains a cycle, the graph-traversal algorithm can avoid
infinite loop.
 We look at two graph-traversal algorithms:
 Depth-First Search
 Breadth-First Search
Graph Traversals
V={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, E={(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3),
(2, 7) , (2, 8), (3, 4) , (3, 9), (4, 10), (5,11), (6, 12), (8, 9), (9, 10), (10, 11) , (12, 8)}
Graph Construction
1
6
4
3
5
2
8
7
9
10
11
12
adjLists
1
2
3
4
5
6
7
8
9
10
11
12 Adjacency List
2 3 4 5 6
3 7 8
4 9
10
11
12
9
10
11
8 Graph
Depth First Search
1
6
4
3
5
2
8
7
9
10
11
12
adjLists
1
2
3
4
5
6
7
8
9
10
11
12 Adjacency List
2 3 4 5 6
3 7 8
4 9
10
11
12
9
10
11
8 Graph
1
6
4
3
5
2
8
7
9
10
11
12
Graph












#include <iostream>
#include <map>
#include <list>
using namespace std;
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
void addEdge(int v, int w); // function to add an edge to graph
void DFS(int v); // DFS traversal of the vertices reachable from v
};
void Graph::addEdge(int v, int w){
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::DFS(int v)
{
visited[v] = true; // Mark the current node as visited and
cout << v << " "; // print it
16
Example 1: Depth First Search
1
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}
int main() {
Graph g; // Create a graph given in the above diagram
int Start = 1;
g.addEdge( 1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(1, 5); g.addEdge(1, 6);
g.addEdge( 2, 3); g.addEdge(2, 7); g.addEdge(2, 8);
g.addEdge( 3, 4); g.addEdge(3, 9);
g.addEdge( 4,10); g.addEdge( 5,11);
g.addEdge( 6,12); g.addEdge( 8, 9);
g.addEdge( 9,10); g.addEdge(10,11); g.addEdge(12, 8);
cout << "Depth First Traversal (starting from vertex "<< Start << " ) n";
g.DFS(Start);
system ("PAUSE"); return 0;
}
17
Example 1: Depth First Search
2
 A systematic search strategy
 Begin at a root node and inspect all the
neighboring nodes
 For each of those neighboring nodes in turn,
inspect their neighboring nodes which were
unvisited, and so on
Algorithm
 Start from node x
 Mark x as visited and Put it a in queue
 While queue is not empty do
 Get a node s from queue and show it
 Get all neighbor vertices of vertex s.
 If a neighbor has not been visited, then
mark it visited and put it in a queue.
Breadth First Search
Starting Point
First Level
Second Level
Breath First Search
Wave Approach
Breadth First Search
1
6
4
3
5
2
8
7
9
10
11
12
adjLists
1
2
3
4
5
6
7
8
9
10
11
12 Adjacency List
2 3 4 5 6
3 7 8
4 9
10
11
12
9
10
11
8 Graph
1
6
4
3
5
2
8
7
9
10
11
12
Graph












Queue push()
pop() 1
2 3 4 5 6
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
6 7 8 9 10 11
7 8 9 10 11
8 9 10 11
9 10 11 12
12
12
10 11 12
11 12
12
#include <iostream>
#include <map>
#include <list>
using namespace std;
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
list<int> queue;
void addEdge(int v, int w);// function to add an edge to graph
void BFS(int v); // BFS traversal of the vertices reachable from v
};
void Graph::addEdge(int v, int w){
adj[v].push_back(w); // Add w to v’s list.
}
20
Example 2: Breadth First Search
1
void Graph::BFS(int v)
{
visited[v] = true; // Mark the current node as visited and
queue.push_back(v); // and put it in a queue
list<int>::iterator i; // 'i' will be used to get all adjacent vertices of a vertex
while(!queue.empty()) {
v = queue.front(); // Get a vertex from queue and print it
queue.pop_front();
cout << v << " ";
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i){
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
21
Example 2: Breadth First Search
2
int main() {
Graph g; // Create a graph given in the above diagram
int Start = 1;
g.addEdge( 1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(1, 5); g.addEdge(1, 6);
g.addEdge( 2, 3); g.addEdge(2, 7); g.addEdge(2, 8);
g.addEdge( 3, 4); g.addEdge(3, 9);
g.addEdge( 4,10); g.addEdge( 5,11);
g.addEdge( 6,12); g.addEdge( 8, 9);
g.addEdge( 9,10); g.addEdge(10,11);
g.addEdge(12, 8);
cout << "Breadth First Traversal (starting from vertex "<< Start << " ) n";
g.BFS(Start);
system ("PAUSE");
return 0;
}
22
Example 2: Breadth First Search
3
 Finding the shortest path out of the maze (unweighted)
 Finding the minimum spanning tree (unweighted)
 Peer to peer (P2P) networks to locate the host
 Facebook to find friends within a given distance
 Web crawler for search engines
Depth First Search (Applications)
 Maze generator by using randomized DFS
 Solving maze puzzles with one solution
 Topological sorting
Breadth First Search (Applications)
 Finding the shortest path out of the maze (unweighted)
 Represent corners, junctions, dead ends by nodes
 Use BFS from a starting point until an exit point is reached
 Start at the root and explore as far as possible along each
branch before backtracking
ALGORITHM dfs(x)
visit(x)
FOR each neighbor y of x DO
IF y was not visited yet THEN
dfs(y)
END
END
END
Maze Solver
Nodes = rooms
Edge = door or passage
 Finding the shortest path out of
the maze (unweighted)
 Represent corners, junctions,
dead ends by nodes
 Use BFS from a starting point
until an exit point is reached
 Start at the root and explore as
far as possible along each
branch before backtracking
ALGORITHM dfs(x)
visit(x)
FOR each neighbor y of x DO
IF y was not visited yet THEN
dfs(y)
END
END
END
Maze Solver (Using BFS)
 Randomly select a starting cell
 Randomly select an unvisited neighbor (randomly choose N, S, E, W)
 Break the wall
 Continue until all cells are visited
Maze Generator (Using Random DFS)
Start
S
E
W
N
S
E
E
N
W
N
E
W
N
S
N
E
W
S
W
S
E
W
E
N
N
S
W
W
E
N
E
S
E
W
W
W
S
S
E
S
W
W
W
N
S
E
W
W
N
E
E
E
S
E
Stop
1
7 8 9 10 11 12
3 4 5 6
2
13
19 20 21 22 23 24
15 16 17 18
14
25 27 28 29 30
26

Data Structures and Agorithm: DS 21 Graph Theory.pptx

  • 1.
    International Islamic UniversityH-10, Islamabad, Pakistan Data Structures Lecture No. 21 Graph, DFS, BFS Engr. Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti
  • 2.
     Graphs arecomposed of Nodes (vertices) and Edges.  A graph is a set of vertices (V) with links or edges (E) connecting them  Linked lists, trees, and heaps are all special cases of graphs  Notation: A graph G is a pair (V, E) i.e G = (V, E)  Each edge is a pair (v1, v2), where v1, v2 are vertices in V  In a directed graph (digraph), all edges have directions  In an undirected graph, an edge does not have a direction  For Example G = (V, E) where V={1, 2, 3, 4, 5, 6} E={(1, 2), (1, 6), (2, 3), (3, 4), (4, 5), (4, 6), (5, 6)} Graphs 2 5 6 1 3 4 Node or Vertex Edge
  • 3.
     If theorder of edge pairs (v1, v2) matters then the graph is directed (also called a digraph): (v1, v2) ≠ (v2, v1)  If the order of edge pairs (v1, v2) does not matter, the graph is called an undirected graph: in this case, (v1 , v2) = (v2 , v1)  Vertex v1 is adjacent to vertex v2, if there is an edge (v1, v2)  In a digraph, existence of edge (v1, v2) does not mean v1 is adjacent to v2  In an undirected graph, existence of edge (v1, v2) means both v1 and v2 are adjacent to each other.  An edge may have a weight or cost Directed vs Undirected Graphs v1 v2 A Directed Graph Undirected Graph v1 v2 v1 v2 15 - 4
  • 4.
     Path: asequence of vertices v1, v2 …, vn such that consecutive vertices vj , vj+1 have an edge between them.  Length: length of a path is the number of edges in the path  Loop: an edge from a vertex onto itself, denoted by (v, v). Definitions v5 v4 v3 v1 v2 Simple Path = v1, v2, v3 v4, v5 Length = 5 v6 Loop (v6, v6)
  • 5.
     Simple Path:a path in a graph is simple if all vertices are distinct  v1 , v3 , v4 (a simple path)  v1 , v3 , v4 , v6 , v3 (a path but not a simple path)  Cycle: a cycle is a path of length at least 1 such that the first and the last vertices are the same.  v1 , v2 , v3 , v1 (A Simple Cycle)  v1 , v2 , v3 , v1 , v1 , v3 (A Cycle but not Simple Cycle) Definitions v5 v4 v3 v1 v2 v6
  • 6.
     A undirectedgraph is connected if there is a path from every vertex to every other vertex. A digraph with this property is strongly connected  If a directed graph is not strongly connected, but underlying undirected graph is connected then the directed graph is weakly connected Definitions v1 v3 v4 v2 strongly connected v1 v3 v4 v2 weakly connected v1 v3 v4 v2 disconnected
  • 7.
    Graph Applications Course Prerequisites Nodes= courses Directed edge = prerequisite Representing a Maze Nodes = rooms Edge = door or passage Representing Electrical Circuits Computer Networks Nodes = battery, switch, resistor, etc. Edges = connections Nodes = computers Edges = transmission rates
  • 8.
     Driving Map Edge = Road  Vertex = Intersection  Edge weight = Time required to cover the road  Airline Traffic  Vertex = Cities serviced by the airline  Edge = Flight exists between two cities  Edge weight = Flight time or flight cost  Computer networks  Vertex = Switches, routers  Edge = Communication links  Edge weight = Delay, hop count, cost Graph Applications  Clash Free Datasheet  Edge = Paper  Vertex = Date  Google Maps  Edge = Roads  Vertex = Location  Dijkstra's Algorithm  Prims's Algorithm  Kruskal's Algorithm
  • 9.
     There areat least two ways of representing graphs:  The adjacency matrix representation  The adjacency list representation  An adjacency matrix for a graph with n vertices numbered 0,1,...,n-1 is an n by n array matrix such that matrix[i][j] is 1 (true) if there is an edge from vertex i to vertex j and 0 (false) otherwise.  When the graph is weighted, we can let matrix[i][j] be the weight that labels the edge from vertex i to vertex j instead of simply 1, and let matrix[i][j] equal to ∞ instead of 0 when there is no edge from vertex i to vertex j  Adjacency matrix for an undirected graph is symmetrical.  i.e. matrix[i][j] is equal to matrix[j][i] Graph Implementation A B D C A Graph A B C D A 0 0 1 1 B 0 0 0 0 C 1 0 0 1 D 1 0 1 0 Adjacency Matrix
  • 10.
     An adjacencylist for a graph with n vertices numbered 0,1,... ,n- 1 consists of n linked lists.  The ith linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j.  Adjacency list is a better solution if the graph is sparse.  Space requirement is O(|E| + |V|), which is linear in the size of the graph. In an undirected graph each edge (v,w) appears in two lists. So space requirement is doubled. Adjacency List A B D C Undirected Weighted Graph A B C D A ∞ ∞ 6 9 B ∞ ∞ ∞ ∞ C 6 ∞ ∞ 8 D 9 ∞ 8 ∞ Its Adjacency Matrix 6 8 9 0 A 1 B 2 C 3 D NULL Adjacency List C 6 A 6 A 9 D 9 D 8 NULL NULL C 8 NULL
  • 11.
    Adjacency Matrix fora Digraph A B D C A Diagraph A B C D A 0 0 1 1 B 0 0 0 0 C 0 0 0 1 D 0 0 0 0 Adjacency Matrix 0 A 1 B 2 C 3 D NULL Adjacency List C D D NULL NULL NULL
  • 12.
     Two commongraph operations: 1. Determine whether there is an edge from vertex i to vertex j. 2. Find all vertices adjacent to a given vertex i.  An adjacency matrix supports operation 1 more efficiently.  An adjacency list supports operation 2 more efficiently.  An adjacency list often requires less space than an adjacency matrix.  Adjacency Matrix: Space requirement is O(|V|2)  Adjacency List: Space requirement is O(|E| + |V|), which is linear in the size of the graph.  Adjacency matrix is better if the graph is dense (too many edges)  Adjacency list is better if the graph is sparse (few edges) Adjacency Matrix vs Adjacency List
  • 13.
     A graph-traversalalgorithm starts from a vertex v, visits all of the vertices that can be reachable from the vertex v.  A graph-traversal algorithm visits all vertices if and only if the graph is connected.  A connected component is the subset of vertices visited during a traversal algorithm that begins at a given vertex.  A graph-traversal algorithm must mark each vertex during a visit and must never visit a vertex more than once.  Thus, if a graph contains a cycle, the graph-traversal algorithm can avoid infinite loop.  We look at two graph-traversal algorithms:  Depth-First Search  Breadth-First Search Graph Traversals
  • 14.
    V={1, 2, 3,4, 5, 6, 7, 8, 9, 10, 11, 12}, E={(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 7) , (2, 8), (3, 4) , (3, 9), (4, 10), (5,11), (6, 12), (8, 9), (9, 10), (10, 11) , (12, 8)} Graph Construction 1 6 4 3 5 2 8 7 9 10 11 12 adjLists 1 2 3 4 5 6 7 8 9 10 11 12 Adjacency List 2 3 4 5 6 3 7 8 4 9 10 11 12 9 10 11 8 Graph
  • 15.
    Depth First Search 1 6 4 3 5 2 8 7 9 10 11 12 adjLists 1 2 3 4 5 6 7 8 9 10 11 12Adjacency List 2 3 4 5 6 3 7 8 4 9 10 11 12 9 10 11 8 Graph 1 6 4 3 5 2 8 7 9 10 11 12 Graph            
  • 16.
    #include <iostream> #include <map> #include<list> using namespace std; class Graph { public: map<int, bool> visited; map<int, list<int> > adj; void addEdge(int v, int w); // function to add an edge to graph void DFS(int v); // DFS traversal of the vertices reachable from v }; void Graph::addEdge(int v, int w){ adj[v].push_back(w); // Add w to v’s list. } void Graph::DFS(int v) { visited[v] = true; // Mark the current node as visited and cout << v << " "; // print it 16 Example 1: Depth First Search 1
  • 17.
    // Recur forall the vertices adjacent to this vertex list<int>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) if (!visited[*i]) DFS(*i); } int main() { Graph g; // Create a graph given in the above diagram int Start = 1; g.addEdge( 1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(1, 5); g.addEdge(1, 6); g.addEdge( 2, 3); g.addEdge(2, 7); g.addEdge(2, 8); g.addEdge( 3, 4); g.addEdge(3, 9); g.addEdge( 4,10); g.addEdge( 5,11); g.addEdge( 6,12); g.addEdge( 8, 9); g.addEdge( 9,10); g.addEdge(10,11); g.addEdge(12, 8); cout << "Depth First Traversal (starting from vertex "<< Start << " ) n"; g.DFS(Start); system ("PAUSE"); return 0; } 17 Example 1: Depth First Search 2
  • 18.
     A systematicsearch strategy  Begin at a root node and inspect all the neighboring nodes  For each of those neighboring nodes in turn, inspect their neighboring nodes which were unvisited, and so on Algorithm  Start from node x  Mark x as visited and Put it a in queue  While queue is not empty do  Get a node s from queue and show it  Get all neighbor vertices of vertex s.  If a neighbor has not been visited, then mark it visited and put it in a queue. Breadth First Search Starting Point First Level Second Level Breath First Search Wave Approach
  • 19.
    Breadth First Search 1 6 4 3 5 2 8 7 9 10 11 12 adjLists 1 2 3 4 5 6 7 8 9 10 11 12Adjacency List 2 3 4 5 6 3 7 8 4 9 10 11 12 9 10 11 8 Graph 1 6 4 3 5 2 8 7 9 10 11 12 Graph             Queue push() pop() 1 2 3 4 5 6 3 4 5 6 7 8 4 5 6 7 8 9 5 6 7 8 9 10 6 7 8 9 10 11 7 8 9 10 11 8 9 10 11 9 10 11 12 12 12 10 11 12 11 12 12
  • 20.
    #include <iostream> #include <map> #include<list> using namespace std; class Graph { public: map<int, bool> visited; map<int, list<int> > adj; list<int> queue; void addEdge(int v, int w);// function to add an edge to graph void BFS(int v); // BFS traversal of the vertices reachable from v }; void Graph::addEdge(int v, int w){ adj[v].push_back(w); // Add w to v’s list. } 20 Example 2: Breadth First Search 1
  • 21.
    void Graph::BFS(int v) { visited[v]= true; // Mark the current node as visited and queue.push_back(v); // and put it in a queue list<int>::iterator i; // 'i' will be used to get all adjacent vertices of a vertex while(!queue.empty()) { v = queue.front(); // Get a vertex from queue and print it queue.pop_front(); cout << v << " "; // Recur for all the vertices adjacent to this vertex list<int>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i){ if (!visited[*i]) { visited[*i] = true; queue.push_back(*i); } } } } 21 Example 2: Breadth First Search 2
  • 22.
    int main() { Graphg; // Create a graph given in the above diagram int Start = 1; g.addEdge( 1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(1, 5); g.addEdge(1, 6); g.addEdge( 2, 3); g.addEdge(2, 7); g.addEdge(2, 8); g.addEdge( 3, 4); g.addEdge(3, 9); g.addEdge( 4,10); g.addEdge( 5,11); g.addEdge( 6,12); g.addEdge( 8, 9); g.addEdge( 9,10); g.addEdge(10,11); g.addEdge(12, 8); cout << "Breadth First Traversal (starting from vertex "<< Start << " ) n"; g.BFS(Start); system ("PAUSE"); return 0; } 22 Example 2: Breadth First Search 3
  • 23.
     Finding theshortest path out of the maze (unweighted)  Finding the minimum spanning tree (unweighted)  Peer to peer (P2P) networks to locate the host  Facebook to find friends within a given distance  Web crawler for search engines Depth First Search (Applications)  Maze generator by using randomized DFS  Solving maze puzzles with one solution  Topological sorting Breadth First Search (Applications)
  • 24.
     Finding theshortest path out of the maze (unweighted)  Represent corners, junctions, dead ends by nodes  Use BFS from a starting point until an exit point is reached  Start at the root and explore as far as possible along each branch before backtracking ALGORITHM dfs(x) visit(x) FOR each neighbor y of x DO IF y was not visited yet THEN dfs(y) END END END Maze Solver Nodes = rooms Edge = door or passage
  • 25.
     Finding theshortest path out of the maze (unweighted)  Represent corners, junctions, dead ends by nodes  Use BFS from a starting point until an exit point is reached  Start at the root and explore as far as possible along each branch before backtracking ALGORITHM dfs(x) visit(x) FOR each neighbor y of x DO IF y was not visited yet THEN dfs(y) END END END Maze Solver (Using BFS)
  • 26.
     Randomly selecta starting cell  Randomly select an unvisited neighbor (randomly choose N, S, E, W)  Break the wall  Continue until all cells are visited Maze Generator (Using Random DFS) Start S E W N S E E N W N E W N S N E W S W S E W E N N S W W E N E S E W W W S S E S W W W N S E W W N E E E S E Stop 1 7 8 9 10 11 12 3 4 5 6 2 13 19 20 21 22 23 24 15 16 17 18 14 25 27 28 29 30 26