Download presentation
Presentation is loading. Please wait.
1
Csc 2720 Instructor: Zhuojun Duan
Data structure Graph Csc Instructor: Zhuojun Duan
2
Outline
3
Outline Definition of graph Variations of graph Implementation
Adjacency matrix Adjacency list Traversal of graph
4
Definition
5
Terminology G = {V, E} A graph G consists of two sets A subgraph
A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset of a graph’s vertices and a subset of its edges Adjacent vertices Two vertices that are joined by an edge
6
Terminology A path between two vertices A simple path A cycle
A sequence of edges that begins at one vertex and ends at another vertex May pass through the same vertex more than once A simple path A path that passes through a vertex only once A cycle A path that begins and ends at the same vertex A simple cycle A cycle that does not pass through a vertex more than once
7
Graph variations A connected graph A disconnected graph
A graph that has a path between each pair of distinct vertices A disconnected graph A graph that has at least one pair of vertices without a path between them A complete graph A graph that has an edge between each pair of distinct vertices
8
Graph variations Multigraph Not a graph
Allows multiple edges between vertices
9
Graph variations Weighted graph
A graph whose edges have numeric labels
10
Graph variations Undirected graph Edges do not indicate a direction
Directed graph, or diagraph Each edge is a directed edge Can have two edges between a pair of vertices, one in each direction Directed path A sequence of directed edges between two vertices Vertex y is adjacent to vertex x if There is a directed edge from x to y
11
Outline Definition of graph Variations of graph Implementation
Adjacency matrix Adjacency list Traversal of graph
12
Operations in a graph Graphs can be used as abstract data types
Two options for defining graphs Vertices contain values Vertices do not contain values Operations of the ADT graph Create an empty graph Determine whether a graph is empty Determine the number of vertices in a graph Determine the number of edges in a graph
13
Operations in a graph Operations of the ADT graph (Continued)
Determine whether an edge exists between two given vertices; for weighted graphs, return weight value Insert a vertex in a graph whose vertices have distinct search keys that differ from the new vertex’s search key Insert an edge between two given vertices in a graph Delete a particular vertex from a graph and any edges between the vertex and other vertices Delete the edge between two given vertices in a graph Retrieve from a graph the vertex that contains a given search key
14
Implementing Graphs Most common implementations of a graph
Adjacency matrix Adjacency list Adjacency matrix for a graph with n vertices numbered 0, 1, …, n – 1 An n by n array matrix such that matrix[i][j] is 1 (or true) if there is an edge from vertex i to vertex j 0 (or false) if there is no edge from vertex i to vertex j
15
Implementing Graphs a) A directed graph and b) its adjacency matrix
16
Implementing Graphs Adjacency matrix for a weighted graph with n vertices numbered 0, 1, …, n – 1 An n by n array matrix such that matrix[i][j] is The weight that labels the edge from vertex i to vertex j if there is an edge from i to j if there is no edge from vertex i to vertex j Diagonal Symmetric matrix a) A weighted undirected graph and b) its adjacency matrix
17
Outline Definition of graph Variations of graph Implementation
Adjacency matrix Adjacency list Traversal of graph
18
Implementing Graphs Adjacency list
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 This node can contain either Vertex j’s value, if any An indication of vertex j’s identity
19
Implementing Graphs a) A directed graph and b) its adjacency list
© 2011 Pearson Addison-Wesley. All rights reserved Implementing Graphs a) A directed graph and b) its adjacency list
20
Implementing Graphs Adjacency list for an undirected graph
Treats each edge as if it were two directed edges in opposite directions a) A weighted undirected graph and b) its adjacency list
21
Implementing Graphs Adjacency matrix compared with adjacency list
Two common operations on graphs 1- Determine whether there is an edge from vertex i to vertex j 2- Find all vertices adjacent to a given vertex i Adjacency matrix Supports operation 1 more efficiently Adjacency list Supports operation 2 more efficiently Often requires less space than an adjacency matrix
22
Outline Definition of graph Variations of graph Implementation
Adjacency matrix Adjacency list Traversal of graph
23
Graph Traversals A graph-traversal algorithm
Visits all the vertices that it can reach Visits all vertices of the graph if and only if the graph is connected A connected component The subset of vertices visited during a traversal that begins at a given vertex Can loop indefinitely if a graph contains a loop To prevent this, the algorithm must Mark each vertex during a visit, and Never visit a vertex more than once
24
Graph Traversals Search vs Traversal Search: Look for a given node
stop when node found, even if not all nodes were visited Traversal: Always visit all nodes Two way of Traversal for graph Depth-first Search (DFS) Breadth-first Search (BFS)
25
Graph Traversals if unvisited
Pseudo-Code for Depth-First Search depth-first-search mark vertex x as visited for each adjacent vertex of x if unvisited do a depth-first search on adjacent vertex
26
First example of DFS
27
Depth-First Search for undirected graph
B C D E F G
28
Depth-First Search for undirected graph
v A B C D E F G A
29
Depth-First Search for undirected graph
v A B C D E F G A
30
Depth-First Search for undirected graph
v A v B C D E F G A B
31
Depth-First Search for undirected graph
v A v B C D E F G A B
32
Depth-First Search for undirected graph
v A v B C D E F G A B
33
Depth-First Search for undirected graph
v A v B C v D E F G A B D
34
Depth-First Search for undirected graph
v A v B C v D E F G A B D
35
Depth-First Search for undirected graph
v A v B C v D E F G A B D
36
Depth-First Search for undirected graph
v A v B C v D E v F G A B D E
37
Depth-First Search for undirected graph
v A v B C v D E v F G A B D E
38
Depth-First Search for undirected graph
v A v B C v D E v F G A B D E
39
Depth-First Search for undirected graph
v A v B C v D E F G A B D E
40
Depth-First Search for undirected graph
v A v B C v D E v F G A B D E
41
Depth-First Search for undirected graph
v A v B C v D E v F G A B D E
42
Depth-First Search for undirected graph
v A v B C v D E v F G A B D E
43
Depth-First Search for undirected graph
v A v B C v D E v F G v A B D E F
44
Depth-First Search for undirected graph
v A v B C v D E v F G v A B D E F
45
Depth-First Search for undirected graph
v A v B C v D E v F G v A B D E F
46
Depth-First Search for undirected graph
v A v v B C v D E v F G v A B D E F C
47
Depth-First Search for undirected graph
v A v v B C v D E v F G v A B D E F C
48
Depth-First Search for undirected graph
v A v v B C v D E v F G v A B D E F C
49
Depth-First Search for undirected graph
v A v v B C v D E v F G v A B D E F C
50
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
51
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
52
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
53
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
54
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
55
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
56
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
57
Depth-First Search for undirected graph
v A v v B C v v D E v F G v A B D E F C G
58
Depth-First Search for undirected graph
B C D E F G A B D E F C G
59
Second example of DFS
60
Task: Conduct a depth-first search of the graph starting with node D
DFS for directed graph by Stack Visited Array F C A B C D E F G H A B D H G E Task: Conduct a depth-first search of the graph starting with node D
61
DFS for directed graph by Stack
Visited Array F C A B C D √ E F G H D A B D H G E The order nodes are visited: D Visit D
62
DFS for directed graph by Stack
Visited Array F C A B C D √ E F G H D A B D H G E The order nodes are visited: D Consider nodes adjacent to D, decide to visit C first (Rule: visit adjacent nodes in alphabetical order)
63
DFS for directed graph by Stack
Visited Array F C A B C √ D E F G H C D A B D H G E The order nodes are visited: D, C Visit C
64
DFS for directed graph by Stack
Visited Array F C A B C √ D E F G H C D A B D H G E The order nodes are visited: D, C No nodes adjacent to C; cannot continue backtrack, i.e., pop stack and restore previous state
65
Back to D – C has been visited, decide to visit E next
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H D A B D H G E The order nodes are visited: D, C Back to D – C has been visited, decide to visit E next
66
Back to D – C has been visited, decide to visit E next
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H E D A B D H G E The order nodes are visited: D, C, E Back to D – C has been visited, decide to visit E next
67
DFS for directed graph by Stack
Visited Array F C A B C √ D E F G H E D A B D H G E The order nodes are visited: D, C, E Only G is adjacent to E
68
DFS for directed graph by Stack
Visited Array F C A B C √ D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G Visit G
69
DFS for directed graph by Stack
Visited Array F C A B C √ D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G Nodes D and H are adjacent to G. D has already been visited. Decide to visit H.
70
DFS for directed graph by Stack
Visited Array F C A B C √ D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H Visit H
71
Nodes A and B are adjacent to F. Decide to visit A next.
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H Nodes A and B are adjacent to F. Decide to visit A next.
72
DFS for directed graph by Stack
Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A Visit A
73
Only Node B is adjacent to A. Decide to visit B next.
DFS for directed graph by Stack Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A Only Node B is adjacent to A. Decide to visit B next.
74
DFS for directed graph by Stack
Visited Array F C A √ B C D E F G H B A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B Visit B
75
No unvisited nodes adjacent to B. Backtrack (pop the stack).
DFS for directed graph Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to B. Backtrack (pop the stack).
76
No unvisited nodes adjacent to A. Backtrack (pop the stack).
DFS for directed graph Visited Array F C A √ B C D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to A. Backtrack (pop the stack).
77
No unvisited nodes adjacent to H. Backtrack (pop the stack).
DFS for directed graph Visited Array F C A √ B C D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to H. Backtrack (pop the stack).
78
No unvisited nodes adjacent to G. Backtrack (pop the stack).
DFS for directed graph Visited Array F C A √ B C D E F G H E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to G. Backtrack (pop the stack).
79
No unvisited nodes adjacent to E. Backtrack (pop the stack).
DFS for directed graph Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to E. Backtrack (pop the stack).
80
F is unvisited and is adjacent to D. Decide to visit F next.
DFS for directed graph Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B F is unvisited and is adjacent to D. Decide to visit F next.
81
DFS for directed graph The order nodes are visited:
Visited Array F C A √ B C D E F G H F D A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F Visit F
82
No unvisited nodes adjacent to F. Backtrack.
DFS for directed graph Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F No unvisited nodes adjacent to F. Backtrack.
83
No unvisited nodes adjacent to D. Backtrack.
DFS for directed graph Visited Array F C A √ B C D E F G H A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F No unvisited nodes adjacent to D. Backtrack.
84
Stack is empty. Depth-first traversal is done.
DFS for directed graph Visited Array F C A √ B C D E F G H A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F Stack is empty. Depth-first traversal is done.
85
Graph Traversals put root node onto a queue
Pseudo-Code for Breadth-First Search (BFS) breadth-first-traversal put root node onto a queue while the queue is not empty dequeue a node from the queue visit the node x e.g., print value enqueue the children of x
86
First example of BFS
87
Breadth-First Search for undirected graph
Queue: A B C Current: D E F G A B C D E F G
88
Breadth-First Search for undirected graph
Queue: A A B C Current: D E F G
89
Breadth-First Search for undirected graph
Queue: A A B C Current: D E F G A
90
Breadth-First Search for undirected graph
Queue: A B C Current: D E F G A A
91
Breadth-First Search for undirected graph
Queue: A B B C Current: D E F G A A
92
Breadth-First Search for undirected graph
Queue: A C B B C Current: D E F G A A
93
Breadth-First Search for undirected graph
Queue: A C B B C Current: D E F G B A
94
Breadth-First Search for undirected graph
Queue: A C B C Current: D E F G B A B
95
Breadth-First Search for undirected graph
Queue: A D C B C Current: D E F G B A B
96
Breadth-First Search for undirected graph
Queue: A E D C B C Current: D E F G B A B
97
Breadth-First Search for undirected graph
Queue: A E D C B C Current: D E F G C A B
98
Breadth-First Search Queue: A E D B C Current: D E F G C A B C
99
Breadth-First Search for undirected graph
Queue: A F E D B C Current: D E F G C A B C
100
Breadth-First Search for undirected graph
Queue: A G F E D B C D E F G Current: C A B C
101
Breadth-First Search for undirected graph
Queue: A G F E D B C D E F G Current: D A B C
102
Breadth-First Search for undirected graph
Queue: A G F E B C D E F G Current: D A B C D
103
Breadth-First Search for undirected graph
Queue: A G F E B C D E F G Current: E A B C D
104
Breadth-First Search for undirected graph
Queue: A G F B C D E F G Current: E A B C D E
105
Breadth-First Search for undirected graph
Queue: A G F B C D E F G Current: F A B C D E
106
Breadth-First Search for undirected graph
Queue: A G B C D E F G Current: F A B C D E F
107
Breadth-First Search for undirected graph
Queue: A G B C D E F G Current: G A B C D E F
108
Breadth-First Search for undirected graph
Queue: A B C D E F G Current: G A B C D E F G
109
Breadth-First Search for undirected graph
A B C D E F G
110
Second example of BFS
111
Breadth-First Search for directed graph
Breadth-first search starts with given node F C A B D H G E Task: Conduct a breadth-first search of the graph starting with node D
112
Breadth-First Search for undirected graph
Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D
113
Breadth-First Search for undirected graph
Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D, C
114
Breadth-First Search for undirected graph
Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D, C, E
115
Breadth-First Search for undirected graph
Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D, C, E, F
116
Breadth-First Search for undirected graph
When all nodes in ripple are visited, visit nodes in next ripples F C A B D H G E 2 1 Nodes visited: D, C, E, F, G
117
Breadth-First Search for undirected graph
When all nodes in ripple are visited, visit nodes in next ripples F C A B D H 3 G E 2 1 Nodes visited: D, C, E, F, G, H
118
Nodes visited: D, C, E, F, G, H, A
Breadth-First Search for undirected graph When all nodes in ripple are visited, visit nodes in next ripples 4 F C A B D H 3 G E 2 1 Nodes visited: D, C, E, F, G, H, A
119
Nodes visited: D, C, E, F, G, H, A, B
Breadth-First Search for undirected graph When all nodes in ripple are visited, visit nodes in next ripples 4 F C A B D H 3 G E 2 1 Nodes visited: D, C, E, F, G, H, A, B
120
© 2011 Pearson Addison-Wesley. All rights reserved
Graph Traversals Visitation order for a) a depth-first search; b) a breadth-first search
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.