Csbp319 Sp22 LCN 9.Pptx (Part One)
Csbp319 Sp22 LCN 9.Pptx (Part One)
Part 1
Graphs
Representations, Traversal Algorithms, Shortest Path Algorithm, MST Algorithm
Lecture Objectives
▪ Graph fundamentals
▪ Graph algorithms:
2
Königsberg – Kaliningrad
3
Königsberg Bridge Problem
▪ In 1736, the following problem was posed:
4
Königsberg Bridge Problem
5
Königsberg Bridge Problem
▪ Starting at one land area, is it possible to walk across all the
bridges exactly once and return to the starting land area?
6
Definitions and Notations
▪ A graph G is a pair G = V, E where
V=
E=
7
Definitions and Notations
▪ Let V G denote the set of vertices, and E G denote the set of
edges of a graph G.
8
Example: Undirected Graphs
9
Example: Undirected Graphs
10
Example: Directed Graphs
11
Example: Directed Graphs
12
Graph Representations
Adjacency Matrix
▪ Let G be a graph with n vertices, where n > 0
▪ Let V G = v1 , v2 , … , vn // {1, 2, … , n}
1 if vi , vj ∈ E G
AG i, j = ቐ
0 if vi , vj ∉ E G
14
Example: Adjacency Matrix
1 2 0 1 1 0
AG = 1 0 0 1
1 0 0 1
0 1 1 0
3 4
15
Example: Adjacency Matrix
16
Adjacency List
▪ For each vertex vi , there is a linked list such that each node of the
linked list contains the vertex u with vi , u ∈ E G
17
Example: Adjacency List
18
Graph Traversals
Lecture Objectives
▪ Graph fundamentals
▪ Graph algorithms:
20
Graph Traversal Algorithms
▪ Traversal algorithms systematically processes all vertices and
edges of a graph:
21
Graph Traversal
• Problem: Search for a certain node or
traverse all nodes in the graph exactly
once.
• Depth First Search
– Once a possible path is found, continue the
search until the end of the path
• Breadth First Search
– Start several paths at a time, and advance in
each one step at a time
Depth First Traversal
CreateStack(S)
Push (S, v) // traversal to start from vertex v
Mark v as visited
While (not (isempty(S)) do
Begin
If all vertices adjacent to the vertex on the top of the stack
had been visited then
Pop (S)
Else
Begin
Select an unvisited vertex u adjacent to the vertex
on the top of the stack
Push (S, u)
Mark u as visited
End
End Slide 28 (of 44)
Depth-first searching
• A depth-first search (DFS)
A explores a path all the way
to a leaf before
B C
backtracking and exploring
another path
• For example, after
D E F G
searching A, then B, then D,
the search backtracks and
H I J K tries another path from B
• Node are explored in the
L M N O P Q order A B D E H L M N I O P
CFGJKQ
• N will be found before J
Breadth-first searching
• A breadth-first search (BFS)
A explores nodes nearest the
root before exploring nodes
B C
further away
• For example, after
searching A, then B, then C,
D E F G
the search proceeds with D,
E, F, G
H I J K • Node are explored in the
order A B C D E F G H I J K L
L M N O P Q MNOPQ
• J will be found before N
DFS-Requirements
a b c d
e f g h
52
Breadth First Traversal
CreateQueue(Q)
Add(Q,v)
Mark v as visited //traversal to start at vertex v
While (not Queueisempty(Q)) do
Begin
v = QueueFront(Q)
Remove (Q)
For each unvisited vertex u adjacent to v do
Begin
Mark u as visited
Add (Q, u)
End
End
Slide 33 (of 44)
BFS - Requirements
B D
H
0
G E
Nodes visited: D
Overview
Breadth-first search starts with
F C given node
A Then visits nodes adjacent in some
specified order (e.g.,
B D alphabetical)
H Like ripples in a pond
0
G E
Nodes visited: D, C
Overview
Breadth-first search starts with
F C given node
A Then visits nodes adjacent in some
specified order (e.g.,
B D alphabetical)
H Like ripples in a pond
0
G E
Nodes visited: D, C, E
Overview
Breadth-first search starts with
F C given node
A Then visits nodes adjacent in some
specified order (e.g.,
B D alphabetical)
H Like ripples in a pond
0
G E
Nodes visited: D, C, E, F
Overview
When all nodes in ripple are visited,
F C visit nodes in next ripples
A
B D
H
0
G E
2 1
Nodes visited: D, C, E, F, G
Overview
When all nodes in ripple are visited,
F C visit nodes in next ripples
A
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G, H
Overview
4 When all nodes in ripple are visited,
F C visit nodes in next ripples
A
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A
Overview
4 When all nodes in ripple are visited,
F C visit nodes in next ripples
A
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A, B
Walk-Through
Enqueued
F C Array
A
A B Q→
B C
D
H D
E
G E
F
G
H