Lec 8
Lec 8
and Computability
Agenda
❑ Introduction (the 7 bridges of Königsberg problem)
❑ What is Graph?
❑ Graph presentation.
❑ Breadth First Search.
❑ Depth First Search.
❑ Dijkstra’s Algorithm.
❑ Kruskal’s Algorithm.
The 7 Bridges of Königsberg
1707 - 1783
Use your worksheet to try and find a route around the centre,
crossing all seven bridges, once and only once. You can start your
journey from any point in the town and you can finish at any point.
? A Vertex
Arc
River Pregel
B
I think you might be
D
I think we
right Peter.
need
another
bridge
Becky. C
Network
The 7 Bridges of Königsberg
odd
odd even
A Vertex
even
Arc
odd
B
D
If a graph has an Eulerian walk then the number of odd
degree vertices is either 0 or 2
C
Simple Graph:
no loops, no multiple edges Ex: Not a simple graph
Loop
Directed Graph: multiple
Each edge has a direction edges
associated to it 1
4
2
• X has degree 5
(number of edges
coming out of it) f
• Parallel edges Y
• h and i are parallel edges
• Adjacent vertices
• Self-loop U and V are adjacent (neighbor)
• j is a self-loop
• Adjacent edges
g and f are adjacent
(as they share the same end point)
Terminology (cont.)
• Path
• sequence of alternating vertices and edges
• begins with a vertex
a
V
b
• ends with a vertex P1
• each edge is preceded and followed by its U
d
X Z
endpoints P2 h
• Simple path
c e
• path such that all its vertices and edges are distinct W g
• Examples
f
• P1=(V,b,X,h,Z) is a simple path Y
• P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not
simple
Properties
Property 1 Notation
v deg(v) = 2m n number of vertices, also called order of graph
Proof: each edge is counted twice m number of edges, also called size of graph
deg(v) degree of vertex v
Property 2
In an undirected graph with no self-
loops and no multiple edges Example
m n (n - 1)/2 ◼ n=4
Proof: each vertex has degree at most ◼ m=6
(n - 1) ◼ deg(v) = 3
Mij=
Aij=
Adjacency Matrix Structure
• Edge list structure
• Augmented vertex objects
• Integer key (index) associated with
vertex
• 2D-array adjacency array
• Reference to edge object for
adjacent vertices
• Null for non nonadjacent vertices
• The “old fashioned” version just has
0 for no edge and 1 for edge
Graph ADT
Graphs 14
Breadth-First Search
L0
A
L1
B C D
L2
E F
Breadth-First Search
Breadth First Search (BFS) algorithm traverses a
graph in a breadthward motion and uses a queue to
remember to get the next vertex to start a search,
when a dead end occurs in any iteration.
As in the example given above, BFS algorithm
traverses from S to A, from S to B, From S to C, it
then goes to D through A, to E through B, to F
through C, and finally goes to G through D. It
employs the following rules:
A unexplored vertex L0
A
A visited vertex
unexplored edge L1
B C D
discovery edge
cross edge E F
L0
L0
A A
L1 L1
B C D B C D
E F E F
B Queue B C D Queue
Example (cont.)
C D Queue
L0 L0
A A
L1 L1
B C D B C D
L2
E F E F
L0 L0
A A
L1 L1
B C D B C D
L2 L2
E F E F 18
C D E Queue D E Queue
Example (cont.)
L0 L0
A A
L1 L1
B C D B C D
L2 L2
E F E F
L0
A
L1
B C D
L2
E
Breadth-First Search F 19
D E F Queue
Properties
Notation BFS Traverse order: A, B, C, D, E, F
B D E
C
Depth-First Search
Depth First Search (DFS) algorithm traverses a
graph in a depthward motion and uses a stack to
remember to get the next vertex to start a search,
when a dead end occurs in any iteration.
As in the example given above, DFS algorithm
traverses from S to A to D to G to E to B first, then
to F and lastly to G. It employs the following rules:
A unexplored vertex A
A visited vertex
unexplored edge B D E
discovery edge
C
back edge
A A
B D E B D E
C
C B C B
A A
Example (cont.)
D
A A C
B
B D E B D E A
C C
A A
D E
B D E B D E
C C
B B
C C
A A
DFS Traverse order: A, B, C, D, E
DFS vs. BFS
L0
A A
L1
B C D B C D
L2
E F E F
DFS BFS
DFS Traverse order: A, B, C, D, F, E BFS Traverse order: A, B, C, D, E, F
Dijkstra's algorithm
• Dijkstra's algorithm: finds shortest (minimum weight) path between a
particular pair of vertices in a weighted directed graph with nonnegative
edge weights
• solves the "one vertex, shortest path" problem
• basic algorithm concept: create a table of information about the
currently known best way to reach each vertex (distance, previous
vertex) and improve it until it reaches the best solution
• in a graph where:
• vertices represent cities,
• edge weights represent driving distances between pairs of cities
connected by a direct road, Dijkstra's algorithm can be used to find
the shortest route between one city and any other
Dijkstra's algorithm
Problem Statement:
For a connected edge weight graph G and a source vertex U V(G). Find the minimum
weight of a path from u to every other vertex in G. Let : E(G) → N be the weight
function
Solution Steps:
1- Set S = which will store the vertices of G for which a shortest path has been found.
2- Set labeling function t(u) = 0 and t(v) = ∞ for all v V(G) v≠u and add u to S
3- Let w be the newest member of S
for each v S v NG(w) set t(v) = min { t(v), t(w) + (vw)}
[hint: NG(w) means the set of all vertices neighbor to the node w]
4- Pick any w S with minimum t(w) and add to S
Repeat step 3
Solution:
For each v S, t(v) is the minimum weight of a path in G with weight function
Example: Initialization
Distance(source) = 0
0 Distance (all vertices but
A
2
B source) =
4 1 3 10
C
2
D
2
E
5 8 4 6 iteration → Initi 1 2 3 4 5
al
A 0
1
F G B ∞
C ∞
D ∞
E ∞
Pick vertex in List with minimum distance. F ∞
G ∞
Example: Update neighbors' distance
0 2
2
A B
4 1 3 10
C
2
D
2
E
5 8 1 4 6
iteration → Initi 1 2 3 4 5
1 al
Distance(B) = 2 F G A 0
Distance(D) = 1 B ∞ 2
C ∞ ∞
D ∞ 1
E ∞ ∞
F ∞ ∞
G ∞ ∞
Example: Remove vertex with minimum
distance
0 2
2
A B
4 1 3 10
C
2
D
2
E
5 8 1 4 6 iteration → Initi 1 2 3 4 5
al
1 A 0
F G B ∞ 2
C ∞ ∞
D ∞ 1
Pick vertex in List with minimum distance, i.e., D E ∞ ∞
F ∞ ∞
G ∞ ∞
Example: Update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
iteration → Initi 1 2 3 4 5
al
1
Distance(C) = 1 + 2 = 3 F G A 0
Distance(E) = 1 + 2 = 3 B ∞ 2
2
Distance(F) = 1 + 8 = 9 9 5 C ∞ ∞ 3
Distance(G) = 1 + 4 = 5 D ∞ 1
E ∞ ∞ 3
F ∞ ∞ 9
G ∞ ∞ 5
Example: Continued...
Pick vertex in List with minimum distance (B) and update neighbors
2 2
3 C D E 3
5 8 1 4 6 iteration → Initi 1 2 3 4 5
al
A 0
1
F G B ∞ 2
2
9 5
C ∞ ∞ 3
D ∞ 1
E ∞ ∞ 3
F ∞ ∞ 9
G ∞ ∞ 5
Example: Continued...
Pick vertex List with minimum distance (E) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
iteration → Initi 1 2 3 4 5
5 8 1 4 6 al
A 0
F
1
G
B ∞ 2
2
C ∞ ∞ 3 3 3
9 5 D ∞ 1
E ∞ ∞ 3 3
F ∞ ∞ 9 9 9
No updating G ∞ ∞ 5 5 5
Example: Continued...
Pick vertex List with minimum distance (C) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
iteration → Initi 1 2 3 4 5
5 8 1 4 6
al
A 0
F
1
G B ∞ 2
2
C ∞ ∞ 3 3 3
8 5 D ∞ 1
E ∞ ∞ 3 3
Distance(F) = 3 + 5 = 8 F ∞ ∞ 9 9 9 8
G ∞ ∞ 5 5 5 5
Example: Continued... iteration → Initi
al
1 2 3 4 5
Pick vertex List with minimum distance (G) and update neighbors A 0
B ∞ 2
2
0 2 C ∞ ∞ 3 3 3
A
2
B D ∞ 1
E ∞ ∞ 3 3
4 1 3 10 F ∞ ∞ 9 9 9 8
2 2
G ∞ ∞ 5 5 5 5
3 C D E 3
5 8 1 4 6
iteration → Initi 1 2 3 4 5 6
al
A 0
1
F G B ∞ 2
2
C ∞ ∞ 3 3 3
6 5 D ∞ 1
Previous distance E ∞ ∞ 3 3
F ∞ ∞ 9 9 9 8 6
Distance(F) = min (8, 5+1) = 6 G ∞ ∞ 5 5 5 5
iteration → Initi 1 2 3 4 5 6
al
A 0
Example (end) B
C
∞
∞
2
∞
2
3 3 3
0 2 D ∞ 1
2
E ∞ ∞ 3 3
A B F ∞ ∞ 9 9 9 8 6
4 1 3 10
G ∞ ∞ 5 5 5 5
2 2
3 C D E 3
5 8 1 4 6
If we ask you to find the
1 shortest path from A to G you
F G
can use this algorithm till you
reach G and stop.
6 5
Pick vertex not in S with lowest cost (F) and update neighbors The cost will be the distance
from A to G = 1+4 = 5
The path will be A→D→G
Minimum spanning tree
• tree: a connected, directed acyclic graph
• spanning tree: a subgraph of a graph, which meets the
constraints to be a tree (connected, acyclic) and connects every
vertex of the original graph
• minimum spanning tree: a spanning tree with weight less than
or equal to any other spanning tree for the given graph
37
Kruskal’s Algorithm
Step 3 - Add the edge which has the least weightage: start adding edges to the graph
beginning from the one which has the least weight. Throughout, we shall keep checking that
the spanning properties remain intact. In case, by adding one edge, the spanning tree
property does not hold then we shall consider not to include the edge in the graph.
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not generate a
cycle
3 edge dv edge dv
F C (D,E) 1 (B,E) 4
A 3 (D,G) 2 (B,F) 4
4
(E,G) 3 (B,H) 4
5
B D (C,D) 3 (A,H) 5
H
}
1 (G,H) 3 (D,F) 6
2 not
3 (C,F) 3 (A,B) 8 considered
G E (B,C) 4 (A,F) 10
Done
Total Cost = dv = 21
Time Complexity
For a graph with E is the number of edges, and V is the number of vertices.