SlideShare a Scribd company logo
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

More Related Content

Similar to Data Structures and Agorithm: DS 21 Graph Theory.pptx

Breadth first search
Breadth first searchBreadth first search
Breadth first search
Sazzad Hossain
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
nabati
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
kailash shaw
 
Semantic Data Management in Graph Databases
Semantic Data Management in Graph DatabasesSemantic Data Management in Graph Databases
Semantic Data Management in Graph Databases
Maribel Acosta Deibe
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
shashankbhadouria4
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
bashirabdullah789
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
ChristianKapsales1
 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
ssuser1989da
 
05 graph
05 graph05 graph
05 graph
Pankaj Prateek
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
Hector Zenil
 
Graphs
GraphsGraphs
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
sahilpawar2426
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Saurabh Kaushik
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
satvikkushwaha1
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
SnehilKeshari
 

Similar to Data Structures and Agorithm: DS 21 Graph Theory.pptx (20)

Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Semantic Data Management in Graph Databases
Semantic Data Management in Graph DatabasesSemantic Data Management in Graph Databases
Semantic Data Management in Graph Databases
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
 
05 graph
05 graph05 graph
05 graph
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graphs
GraphsGraphs
Graphs
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 

Recently uploaded

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
mahaffeycheryld
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
ijaia
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 

Recently uploaded (20)

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 

Data Structures and Agorithm: DS 21 Graph Theory.pptx

  • 1. 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
  • 2.  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
  • 3.  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
  • 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)
  • 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 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
  • 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 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
  • 10.  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
  • 11. 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
  • 12.  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
  • 13.  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
  • 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 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            
  • 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 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
  • 18.  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
  • 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 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
  • 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() { 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
  • 23.  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)
  • 24.  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
  • 25.  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)
  • 26.  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