0% found this document useful (0 votes)
6 views

Lec 8

This document discusses algorithms and data structures for graphs. It begins with an introduction to the Königsberg bridge problem solved by Euler that helped establish graph theory. It then defines what a graph is and discusses different graph representations like the adjacency matrix. Various graph algorithms are outlined like breadth-first search, depth-first search, Dijkstra's algorithm, and Kruskal's algorithm. Vertex degrees and Euler circuits and paths are also defined.

Uploaded by

hussienboss99
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)
6 views

Lec 8

This document discusses algorithms and data structures for graphs. It begins with an introduction to the Königsberg bridge problem solved by Euler that helped establish graph theory. It then defines what a graph is and discusses different graph representations like the adjacency matrix. Various graph algorithms are outlined like breadth-first search, depth-first search, Dijkstra's algorithm, and Kruskal's algorithm. Vertex degrees and Euler circuits and paths are also defined.

Uploaded by

hussienboss99
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/ 54

Algorithms, Data Structures

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

Leonhard Euler, (pronounced “oiler”) was a Swiss


mathematician whose power of mental calculation was
prodigious. As a teenager his fame quickly spread
throughout Europe and at the age of 20 he was
summoned by Catherine 1st of Russia to study at the
Academy of Science at St Petersburg. He became
Professor of Physics at 23 and Professor of
mathematics at 26. At the Academy he contributed to
numerous branches of mathematics and other sciences,
1707 - 1783
including work on optics and the study of planetary
motion. He made progress towards solving “Fermat’s
Last Theorem” and introduced the notation for Pi ()
and sigma (). At 31 he lost the sight in his right eye
and later became almost totally blind. He had thirteen
children of whom only five survived their infancy. His
life’s work of 886 books/papers was still being
Euler
published 50 years after his death.
The 7 Bridges of Königsberg

Whilst working at the academy Euler got to hear of the


problem of the 7 Bridges of Königsberg. The river
Pregel ran through the town and divided it into 4 land
areas, one of which was an island in the middle. The
river was spanned by seven bridges and the people of
the town tried to walk a route around the centre that
crossed all seven bridges once and only once.

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.
The 7 Bridges of Königsberg

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.

This is equivalent to drawing


the network on paper without
lifting your pencil or going
over a line once drawn.

? 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

During his investigation Euler defined an


even vertex and an odd vertex. An odd
vertex has an odd number of arcs
connected to it and an even vertex has
an even number of arcs connected to it.

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

Network Start = end Start ≠ end


Euler circuits and paths

• An Euler circuit in a graph is a simple circuit containing every edge of the


graph.
• An Euler path in a graph is a simple path containing every edge of the
graph.
• Theorem 1: A finite graph has an Euler circuit if and only if it is
connected and all vertices have even degree.
• Theorem 2: A finite graph has an Euler path (but not an Euler circuit) if
and only if it is connected and exactly two vertices have odd degrees.
7
What is a Graph?
A Graph G is an order pair G= (V, E) where V is a (finite) set of elements
and E is a set of 2-subsets of V
1
V = {1,2,3}
E= { {1,2}, {3,2}, {1,3} }
2 3

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

The edges are presented in order pairs: 3


E= {(1,3), (3,2), (1,2){
Terminology
V
a b
• End vertices (or endpoints) of an edge h j
• U and V are the endpoints of a
U d X Z
• Edges incident on a vertex
c e i
• a, d, and b are incident on V
• Degree of a vertex
W g

• 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

What is the bound for a directed


graph? |V| = order of the graph
The ways to represent a Graph

Adjacency Matrix Incidence Matrix

1 if ij  E 1 if vertex i belongs to edge j


Aij= Mij=
0 otherwise 0 otherwise
The “old
Rows represent vertices
fashioned”
Columns represent edges
version just
Aij=
has 0 for no
edge and 1
for edge

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:

Rule 1 − Visit the adjacent unvisited vertex. Mark it


as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the
first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is
empty.

BFS on a graph with n vertices and m edges takes


O(n + m ) time
Example B C Queue

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

Gs: connected component of s A


Property 1
BFS(G, s) visits all the vertices and edges of B C D
Gs
Property 2 E F

The discovery edges labeled by BFS(G, s)


form a spanning tree Ts of Gs
L0
Property 3 A

For each vertex v in Li L1


• The path of Ts from s to v has i edges B C D

• Every path from s to v in Gs has at least i L2


edges E F
Depth-First Search
A

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:

Rule 1 − Visit the adjacent unvisited vertex. Mark


it as visited. Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a
vertex from the stack. (It will pop up all the
vertices from the stack, which do not have
adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until the stack
is empty.

DFS on a graph with n vertices and m edges


takes O(n + m ) time
Example

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

0 2 Note : distance(D) not updated


2 since D is already known and
A B
distance(E) not updated since it is
larger than previously computed
4 1 3 10

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

Work with edges, rather than nodes


Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not generate a
cycle
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy approach.
This algorithm treats the graph as a forest and every node it has as an individual tree.
A tree connects to another only and only if, it has the least cost among all available
options and does not violate MST properties. …MST: minimum spanning tree
Kruskal’s Algorithm step by step
Step 1 - Remove all loops and Parallel Edges
Remove all loops and parallel edges from the given
Consider an undirected, weight graph graph. In case of parallel edges, keep the one which
has the least cost associated and remove all others.

3 Step 2 - Arrange all edges in their increasing order


10
F C of weight: create a set of edges and weight, and
arrange them in an ascending order of weightage
A 4
4
3
(cost).
8
6
5
4
B D
4 Step 3 - Add the edge which has the least
H 1 weightage: start adding edges to the graph
2
3 beginning from the one which has the least weight.
G 3
E Throughout, we shall keep checking that the
spanning properties remain intact. In case, by
8 adding one edge, the spanning tree property does
7
not hold then we shall consider not to include the
edge in the graph.
Step 2 - Arrange all edges in their
increasing order of weight:

Sort the edges by increasing edge weight


3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
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
8 4
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

Step 3 - Add the edge which has the least weightage


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

Accepting edge (E,G) would create a cycle

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.

Time complexity of the Dijkstra algorithm:


Time Complexity of the Dijkstra algorithm is O(V^2) if it is implemented using
“the adjacency matrix representation”. However, this complexity is reduced to
O(E Log V) if the Binary Heap is implemented.

Time complexity of the Kruskal algorithm:


Time Complexity: O(ElogE) or O(ElogV). Sorting of edges takes O(ELogE) time.
After sorting, we iterate through all edges and apply find-union algorithm. The find
and union operations can take atmost O(LogV) time. So overall complexity is
O(ELogE + ELogV) time. The value of E can be atmost O(V2), so O(LogV) are
O(LogE) same. Therefore, overall time complexity is O(ElogE) or O(ElogV)
Thank You

You might also like