0% found this document useful (0 votes)
25 views75 pages

Csbp319 Sp22 LCN 9.Pptx (Part One)

This document discusses graph representations and traversal algorithms. It introduces graphs and how they can be represented using adjacency matrices and adjacency lists. It also explains depth-first search and breadth-first search traversal algorithms, including examples of how each algorithm traverses and explores nodes in a graph.

Uploaded by

19saeedalawi96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views75 pages

Csbp319 Sp22 LCN 9.Pptx (Part One)

This document discusses graph representations and traversal algorithms. It introduces graphs and how they can be represented using adjacency matrices and adjacency lists. It also explains depth-first search and breadth-first search traversal algorithms, including examples of how each algorithm traverses and explores nodes in a graph.

Uploaded by

19saeedalawi96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Lecture 9

Part 1

Graphs
Representations, Traversal Algorithms, Shortest Path Algorithm, MST Algorithm
Lecture Objectives
▪ Graph fundamentals

▪ Graph algorithms:

• Traversal algorithms: DFS & BFS

• Shortest path algorithm

• Minimal spanning tree algorithm

2
Königsberg – Kaliningrad

3
Königsberg Bridge Problem
▪ In 1736, the following problem was posed:

• River Pregel (Pregolya) flows around the islands Kneiphof &


Lomse

• divides into four land areas

• connected with seven bridges

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?

▪ In 1736, Euler represented Konigsberg bridge problem as graph


and answered the question in the negative.

▪ This marked (as recorded) the birth of graph theory.

6
Definitions and Notations
▪ A graph G is a pair G = V, E where

• V is a finite nonempty set, called the set of vertices/nodes of G

• E⊆V×V= x, y ∶ x, y ∈ V is the set of edges

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.

▪ If the elements of E G are ordered pairs, G is called a directed


graph or digraph; Otherwise, G is called an undirected graph.

▪ In an undirected graph, the pairs u, v and v, u represent the


same edge.

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}

▪ The adjacency matrix AG is a two-dimensional n × n matrix such


that
• the i, j th entry of AG is 1 if there is an edge from vi to vj ;

• otherwise, the i, j th entry is 0

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

▪ An array A is used where A[i] is a reference variable


pointing to the first node of the linked list containing the vertices
to which vi is adjacent.

▪ Each node has two components vertex and link

▪ Component vertex contains index of vertex adjacent to vertex vi

17
Example: Adjacency List

18
Graph Traversals
Lecture Objectives
▪ Graph fundamentals

▪ Graph algorithms:

• Traversal algorithms: DFS & BFS

• Shortest path algorithm

• Minimal spanning tree algorithm

20
Graph Traversal Algorithms
▪ Traversal algorithms systematically processes all vertices and
edges of a graph:

• Depth First Search (Traversal) (DFS)

• Breadth First Search (traversal) (BFS)

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

• Also called Depth-First Search


• Can be used to attempt to visit all nodes
of a graph in a systematic manner
• Works with directed and undirected
graphs
• Works with weighted and unweighted
graphs
Walk-Through
Visited Array
F C A
A B
B C
D
H D
E
G E
F
G
H

Task: Conduct a depth-first search of the graph


starting with node D
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
G E
F
G
H D
The order nodes are visited:
D Visit D
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
G E
F
G
H D
The order nodes are visited:
Consider nodes adjacent to D, decide
D to visit C first (Rule: visit adjacent
nodes in alphabetical order)
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
G C
H D
The order nodes are visited:
Visit C
D, C
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
G C
H D
The order nodes are visited:
No nodes adjacent to C; cannot
D, C continue ➔ backtrack, i.e., pop
stack and restore previous state
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
G
H D
The order nodes are visited:
Back to D – C has been visited, decide
D, C to visit E next
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E
F
G E
H D
The order nodes are visited:
Back to D – C has been visited, decide
D, C, E to visit E next
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E
F
G E
H D
The order nodes are visited:
Only G is adjacent to E
D, C, E
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
G √ E
H D
The order nodes are visited:
Visit G
D, C, E, G
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
G √ E
H D
The order nodes are visited:
Nodes D and H are adjacent to G. D
D, C, E, G has already been visited. Decide to
visit H.
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit H
D, C, E, G, H
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Nodes A and B are adjacent to F.
D, C, E, G, H Decide to visit A next.
Walk-Through
Visited Array
F C A √
A B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit A
D, C, E, G, H, A
Walk-Through
Visited Array
F C A √
A B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Only Node B is adjacent to A. Decide
D, C, E, G, H, A to visit B next.
Walk-Through
Visited Array
F C A √
A B √ B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit B
D, C, E, G, H, A, B
Walk-Through
Visited Array
F C A √
A B √
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to B.
D, C, E, G, H, A, B Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to A.
D, C, E, G, H, A, B Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to H.
D, C, E, G, H, A, B Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to G.
D, C, E, G, H, A, B Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to E.
D, C, E, G, H, A, B Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √
H √ D
The order nodes are visited:
F is unvisited and is adjacent to D.
D, C, E, G, H, A, B Decide to visit F next.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √ F
H √ D
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to F.
D, C, E, G, H, A, B, F Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to D.
D, C, E, G, H, A, B, F Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first traversal is
D, C, E, G, H, A, B, F done.
Depth-First Search - Exercise

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

• Can be used to attempt to visit all nodes


of a graph in a systematic manner
• Works with directed and undirected
graphs
• Works with weighted and unweighted
graphs
Overview
Breadth-first search starts with
F C given node
A

B D
H
0
G E

Task: Conduct a breadth-first search of the graph


starting with node 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
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

How is this accomplished? Simply replace the stack with a


queue! Rules: (1) Maintain an enqueued array. (2) Visit node
when dequeued.
Walk-Through
Enqueued
F C Array
A
A B Q→D
B C
D
H D √
E
G E
F
G
Nodes visited: H

Enqueue D. Notice, D not yet visited.


Walk-Through
Enqueued
F C Array
A A Q→C→E→F
B B
D
H C √
D √
G E
E √
F √
Nodes visited: D G
H
Dequeue D. Visit D. Enqueue unenqueued nodes adjacent to
D.
Walk-Through
Enqueued
F C Array
A
A B Q→E→F
B C √
D
H D √
E √
G E
F √
G
Nodes visited: D, C H

Dequeue C. Visit C. Enqueue unenqueued nodes adjacent to


C.
Walk-Through
Enqueued
F C Array
A
A B Q→F→G
B C √
D
H D √
E √
G E
F √
G
Nodes visited: D, C, E H

Dequeue E. Visit E. Enqueue unenqueued nodes adjacent to


E.
Walk-Through
Enqueued
F C Array
A
A B Q→G
B C √
D
H D √
E √
G E
F √
G √
Nodes visited: D, C, E, F H

Dequeue F. Visit F. Enqueue unenqueued nodes adjacent to F.


Walk-Through
Enqueued
F C Array
A
A B Q→H
B C √
D
H D √
E √
G E
F √
G √
Nodes visited: D, C, E, F, G H √

Dequeue G. Visit G. Enqueue unenqueued nodes adjacent to


G.
Walk-Through
Enqueued
F Array
C A √
A B √ Q→A→B
B C √
D
H D √
E √
G E
F √
G √
Nodes visited: D, C, E, F, G, H H √

Dequeue H. Visit H. Enqueue unenqueued nodes adjacent to


H.
Walk-Through
Enqueued
F Array
C A √
A B √ Q→B
B C √
D
H D √
E √
G E
F √
G √
Nodes visited: D, C, E, F, G, H, A H √

Dequeue A. Visit A. Enqueue unenqueued nodes adjacent to


A.
Walk-Through
Enqueued
F Array
C A √
A B √ Q empty
B C √
D
H D √
E √
G E
F √
G √
Nodes visited: D, C, E, F, G, H, A, B H √

Dequeue B. Visit B. Enqueue unenqueued nodes adjacent to


B.
Walk-Through
Enqueued
F Array
C A √
A B √ Q empty
B C √
D
H D √
E √
G E
F √
G √
Nodes visited: D, C, E, F, G, H, A, B H √

Q empty. Algorithm done.


Summary
▪What are graphs
▪Types of graphs
▪Graph representation
▪Adjacency matrix
▪Adjacency lists
▪Depth and breadth first graph traversals
▪Depth and breadth first search

Slide 43 (of 44)

You might also like