Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 71

DATA STRUCTURES AND ALGORITHMS

CHAPTER 07- GRAPHS

1
WHAT IS A GRAPH?
 A graph G = (V,E) is composed of:

V: set of vertices
E: set of edges connecting the vertices in V
 An edge e = (u,v) is a pair of vertices
V= {a,b,c,d,e}
 Example:

E= {(a,b),(a,c),
a b (a,d),
(b,e),(c,d),(c,e),
(d,e)}
c

d e
2
APPLICATIONS
CS16
 electronic circuits

 networks (roads, flights, communications)

JFK

LAX
LAX STL
HNL
DFW
FTL 3
TERMINOLOGY: ADJACENT AND INCIDENT

 If (v0, v1) is an edge in an undirected graph,

 v0 and v1 are adjacent

 The edge (v0, v1) is incident on vertices v0 and v1

 If <v0, v1> is an edge in a directed graph

 v0 is adjacent to v1, and v1 is adjacent from v0

 The edge <v0, v1> is incident on v0 and v1

4
TERMINOLOGY: DEGREE OF A VERTEX
 The degree of a vertex is the number of edges incident to that vertex
 For directed graph,
 the in-degree of a vertex v is the number of edges
that have v as the head
 the out-degree of a vertex v is the number of edges
that have v as the tail
 if di is the degree of a vertex i in a graph G with n vertices and e
edges, the number of edges is

Why? Since adjacent vertices each


count the adjoining edge, it will be
n 1 counted twice
e( d )/ 2
0
i

5
EXAMPLES
0
3 2
0 1 2
3 3
3 1 2 3 3 4 5 6
3G1 1 1 G2 1 1
3
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2

2 in: 1, out: 0
G3
6
TERMINOLOGY: PATH

path: sequence of vertices


v1,v2,. . .vk such that 3 2
consecutive vertices vi and
vi+1 are adjacent. 3

3 3

a b a b

c c

d e d e
abedc bedc
7
MORE TERMINOLOGY

a b
simple path: no repeated vertices
bec
c

d e

a b
cycle: simple path, except that the last vertex is the same as the first
acda
vertex c

d e
8
EVEN MORE TERMINOLOGY
• connected graph: any two vertices are connected by some path

connected not connected


 subgraph: subset of vertices and edges forming a graph
 connected component: maximal connected subgraph. E.g., the graph below has 3
connected components.

9
SUB-GRAPHS EXAMPLES
0
0 0 1 2 0
1 2
1 2 3 1 2
3
G1 (i) (ii) iii) 3 (iv)
(a) Some of the subgraph of G1
0
0 0 0 0

1 1 1 1

2 2 2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G2
G2 10
MORE…
tree - connected graph without cycles
forest - collection of trees
tree

tree

forest

tree

tree

11
CONNECTIVITY
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are
adjacent
How many total edges in a complete graph?
 Each of the n vertices is incident to n-1 edges, however, we would have
counted each edge twice! Therefore, intuitively, m = n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2

n 5
m  (5 

12
MORE CONNECTIVITY

If m < n - 1, G is not
connected
n5
n = #vertices
m4
m = #edges
 For a tree m = n - 1

n5
m3

13
ORIENTED (DIRECTED) GRAPH

 A graph where edges are directed

14
DIRECTED VS. UNDIRECTED GRAPH
An undirected graph is one in which the pair of vertices in a
edge is unordered, (v0, v1) = (v1,v0)
A directed graph is one in which each edge is a directed pair
of vertices, <v0, v1> != <v1,v0>

tail head

15
IMPLEMENT A GRAPH

 To implement a graph, you need to first represent the given information in

the form of a graph.

 The two most commonly used ways of representing a graph are as follows:

 Adjacency Matrix

 Adjacency List

16
ADJACENCY MATRIX

Consider the following Adjacency Matrix Representation


graph:
v1 v2 v3 v4

v1 0 1
0 0
v2 0 0
1 0
v3 0 0
0 0
v4 1 0
1 0
17
ADJACENCY LIST

Consider the following Adjacency List Representation


graph:

18
TRAVERSING A GRAPH

 Traversing a graph means visiting all the vertices in a graph.

 You can traverse a graph with the help of the following two methods:

 Depth First Search (DFS)

 Breadth First Search (BFS)

19
DEPTH FIRST SEARCH (DFS)

Algorithm: DFS(v)
1. Push the starting vertex, v into the stack.
2. Repeat until the stack becomes empty:

a. Pop a vertex from the stack.


b. Visit the popped vertex.
c. Push all the unvisited vertices adjacent to the popped vertex
into the stack.

20
DFS(CONTD.)
Push the starting vertex, v1 into the stack

v1

21
DFS(CONTD.)
 Pop a vertex, v1 from the stack
 Visit v1
 Push all unvisited vertices adjacent to v1 into the stack

v1

v1
22
DFS(CONTD.)
 Pop a vertex, v1 from the stack
 Visit v1
 Push all unvisited vertices adjacent to v1 into the stack

v2
v4

v1 23
DFS (CONTD.)
 Pop a vertex, v2 from the stack
 Visit v2
 Push all unvisited vertices adjacent to v2 into the stack

v2
v4

v1 v2 24
DFS (Contd.)
 Pop a vertex, v2 from the stack
 Visit v2
 Push all unvisited vertices adjacent to v2 into the stack

v6
v3
v4

Visited:
v1 v2
25
DFS (Contd.)
 Pop a vertex, v6 from the stack
 Visit v6
 Push all unvisited vertices adjacent to v6 into the stack

v6
v3
v4

There are no unvisited vertices


adjacent to v6
Visited:
v1 v2 v6
26
DFS (Contd.)
 Pop a vertex, v3 from the stack
 Visit v3
 Push all unvisited vertices adjacent to v3 into the stack

v3
v4

Visited:
v1 v2 v6 v3
27
DFS (Contd.)
 Pop a vertex, v3 from the stack
 Visit v3
 Push all unvisited vertices adjacent to v3 into the stack

v5
v4

Visited:
v1 v2 v6 v3
28
DFS (Contd.)
 Pop a vertex, v5 from the stack
 Visit v5
 Push all unvisited vertices adjacent to v5 into the stack

v5
v4

There are no unvisited vertices


adjacent to v5
Visited:
v1 v2 v6 v3 v5
29
DFS (Contd.)
 Pop a vertex, v4 from the stack
 Visit v4
 Push all unvisited vertices adjacent to v4 into the stack

v4

There are no unvisited vertices


adjacent to v4
Visited:
v1 v2 v6 v3 v5 v4
30
DFS (Contd.)
 The stack is now empty
 Therefore, traversal is complete

Visited:
v1 v2 v6 v3 v5 v4
31
DFS (Contd.)
 Although the preceding algorithm provides a simple and convenient method
to traverse a graph, the algorithm will not work correctly if the graph is not
connected.
 In such a case, you will not be able to traverse all the vertices from one
single starting vertex.

32
DFS (Contd.)

 To solve this problem, you need to execute 1. Repeat step 2 for each vertex, v
the preceding algorithm repeatedly for all in the graph
unvisited vertices in the graph. 2. If v is not visited:
a. Call DFS(v)

33
BFS

Algorithm: BFS(v)
1. Visit the starting vertex, v and insert it into a
queue.
2. Repeat step 3 until the queue becomes empty.
3. Delete the front vertex from the queue, visit all its
unvisited adjacent vertices, and insert them into the
queue.

34
BFS (Contd.)
 Visit v1
 Insert v1 into the queue

v1

v1
35
BFS (Contd.)
 Remove a vertex v1 from the queue
 Visit all unvisited vertices adjacent to v1 and insert them in the queue

v1

Visited:
v1
36
BFS (Contd.)
 Remove a vertex v1 from the queue
 Visit all unvisited vertices adjacent to v1 and insert them in the queue

v2 v4

v1 v2 v4
37
BFS (Contd.)
 Remove a vertex v2 from the queue
 Visit all unvisited vertices adjacent to v2 and insert them in the queue

v2 v4

v1 v2 v4
38
BFS (Contd.)
 Remove a vertex v2 from the queue
 Visit all unvisited vertices adjacent to v2 and insert them in the queue

v4 v3 v6

v1 v2 v4 v3 v6
39
BFS (Contd.)
 Remove a vertex v4 from the queue
 Visit all unvisited vertices adjacent to v4 and insert them in the queue

v4 v3 v6 v5

v1 v2 v4 v3 v6 v5
40
BFS (Contd.)
 Remove a vertex v3 from the queue
 Visit all unvisited vertices adjacent to v3 and insert them in the queue

v3 v6 v5

v3 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
41
BFS (Contd.)
 Remove a vertex v6 from the queue
 Visit all unvisited vertices adjacent to v6 and insert them in the queue

v6 v5

v3 does not have any


unvisited adjacent
vertices

v1 v2 v4 v3 v6 v5
42
BFS (Contd.)
 Remove a vertex v6 from the queue
 Visit all unvisited vertices adjacent to v6 and insert them in the queue

v5

v6 does not have any unvisited


adjacent vertices

v1 v2 v4 v3 v6 v5
43
BFS (Contd.)
 Remove a vertex v5 from the queue
 Visit all unvisited vertices adjacent to v5 and insert them in the queue

v5

v6 does not have any


unvisited adjacent
vertices

v1 v2 v4 v3 v6 v5
44
BFS (Contd.)
 Remove a vertex v5 from the queue
 Visit all unvisited vertices adjacent to v5 and insert them in
the queue

v5 does not have any


unvisited adjacent
vertices

v1 v2 v4 v3 v6 v5
45
BFS (Contd.)
 The queue is now empty
 Therefore, traversal is complete

v5 does not have any


unvisited adjacent
vertices

v1 v2 v4 v3 v6 v5
46
BFS (Contd.)
 Although the preceding algorithm provides a simple and
convenient method to traverse a graph, the algorithm will not
work correctly if the graph is not connected.
 In such a case, you will not be able to traverse all the vertices
from one single starting vertex.

47
BFS (Contd.)

 To solve this problem, you need to execute the 1. Repeat step 2 for each
vertex, v in the graph
preceding algorithm repeatedly for all unvisited
2. If v is not visited:
vertices in the graph. a. Call BFS(v)

48
Activity: Implementing a Graph by Using Adjacency Matrix Representation

 Problem Statement:
 Represent Ethiopian regional capital cities including Addis Ababa and
the distances between them in the form of a graph. Write a program to
represent the graph in the form of an adjacency matrix.

49
Applications of Graphs
 Many problems can be easily solved by reducing them in the form of a
graph
 Graph theory has been instrumental in analysing and solving problems in
areas as diverse as computer network design, urban planning, finding
shortest paths and molecular biology.

50
Solving the Shortest Path Problem
 The shortest path problem can be solved by applying the Dijkstra’s
algorithm on a graph
 The Dijkstra’s algorithm is based on the greedy approach
 The steps in the Dijkstra’s algorithm are as follows:
1. Choose vertex v corresponding to the smallest distance
recorded in the DISTANCE array such that v is not already in
FINAL.
2. Add v to FINAL.
3. Repeat for each vertex w in the graph that is not in FINAL:
a. If the path from v1 to w via v is shorter than the previously
recorded distance from v1 to w (If ((DISTANCE[v] + weight of
edge(v,w)) < DISTANCE[w])):
i. Set DISTANCE[w]=DISTANCE[v] + weight of edge(v,w).
4. If FINAL does not contain all the vertices, go to step 1.
51
Solving the Shortest Path Problem (Contd.)
5 Suppose you need to find the
shortest distance of all the vertices
from vertex v1.
3 4 6
Add v1 to the FINAL array.

2
3

6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 ∞ 3 ∞ ∞

FINAL v1

52
Solving the Shortest Path Problem (Contd.)
5 In the DISTANCE array, vertex
v4 has the shortest distance from
vertex v1.
3 4 6 Therefore, v4 is added to the
FINAL array.
2
3

6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 ∞ 3 ∞ ∞

FINAL v1 v4

53
Solving the Shortest Path Problem (Contd.)
5
v1 → v2 = 5
v1 → v4 → v2 = 3 + ∞ = ∞

3 4 6
∞>5
Therefore, no change is made.
2
3

6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 ∞ 3 ∞ ∞

FINAL v1 v4

54
Solving the Shortest Path Problem (Contd.)
5
v1 → v3 = ∞
v1 → v4 → v3 = 3 + 2 = 5

3 4 6
5<∞
Therefore, the entry
2
3 corresponding to v3 in the
DISTANCE array is changed to
6 3
5.

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5
∞ 3 ∞ ∞

FINAL v1 v4

55
Solving the Shortest Path Problem (Contd.)
5
v1 → v5 = ∞
v1 → v4 → v5 = 3 + 6 = 9

3 4 6
9<∞
Therefore, the entry
2
3 corresponding to v5 in the
DISTANCE array is changed to
6 3
9.

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 ∞
9 ∞

FINAL v1 v4

56
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = ∞
v1 → v4 → v6 = 3 + ∞ = ∞

3 4 6

Both the values are equal. Therefore,


2
3 no change is made.
6 3
PASS 1 complete

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞

FINAL v1 v4

57
Solving the Shortest Path Problem (Contd.)
5
From the DISTANCE array, select
the vertex with the shortest
distance from v1, such that the
selected vertex is not in the
3 4 6
FINAL array.

2
v2 and v3 have the shortest and the
3
same distance from v1.
6 3 Let us select v2 and add it to the
FINAL array.

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞

FINAL v1 v4 v2

58
Solving the Shortest Path Problem (Contd.)
5
v1 → v3 = 5
v1 → v2 → v3 = 5 + 4 = 9

3 4 6
9>5
Therefore, no change is made.
2
3

6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞

FINAL v1 v4 v2

59
Solving the Shortest Path Problem (Contd.)
5
v1 → v5 = 9
v1 → v2 → v5 = 5 + ∞ = ∞

3 4 6
∞>9
Therefore, no change is made.
2
3

6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞

FINAL v1 v4 v2

60
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = ∞
v1 → v2 → v6 = 5 + 6 = 11

3 4 6
11 < ∞
Therefore, the entry
2
3 corresponding to v6 in the
DISTANCE array is changed to
6 3
11.
Pass 2 complete

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 ∞
11

FINAL v1 v4 v2

61
Solving the Shortest Path Problem (Contd.)
5
From the DISTANCE array, select
the vertex with the shortest
distance from v1, such that the
selected vertex is not in the
3 4 6
FINAL array.

2 Let us select v3 and add it to the


3
FINAL array.
6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 11

FINAL v1 v4 v2 v3

62
Solving the Shortest Path Problem (Contd.)
5
v1 → v5 = 9
v1 → v3 → v5 = 5 + 3 = 8

3 4 6
8<9
Therefore, the entry corresponding
2
3 to v5 in the DISTANCE array is
changed to 8.
6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 98 11

FINAL v1 v4 v2 v3

63
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = 11
v1 → v3 → v6 = 5 + 3 = 8

3 4 6
8 < 11
Therefore, the entry
2
3 corresponding to v6 in the
DISTANCE array is changed to 8.
6 3
Pass 3 complete

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 11
8

FINAL v1 v4 v2 v3

64
Solving the Shortest Path Problem (Contd.)
5
From the DISTANCE array, select
the vertex with the shortest
distance from v1, such that the
selected vertex is not in the
3 4 6
FINAL array.

2 Let us select v5 and add it to the


3
FINAL array.
6 3

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8

FINAL v1 v4 v2 v3 v5

65
Solving the Shortest Path Problem (Contd.)
5
v1 → v6 = 8
v1 → v5 → v6 = 8 + ∞ = ∞

3 4 6
∞>8
Therefore, no change is made.
2
3

6 3
Pass 4 complete

v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8

FINAL v1 v4 v2 v3 v5

66
Solving the Shortest Path Problem (Contd.)

5
Now add the only remaining
vertex, v6 to the FINAL array.

3 4 6 All vertices have been added to


the FINAL array.
2
This means that the
3
DISTANCE array now
6 3
contains the shortest distances
from vertex v1 to all other
vertices.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8

FINAL v1 v4 v2 v3 v5 v6

67
PRIM’S ALGORITHM

Finding a Minimum Spanning Tree (MST)

Step 1: x  V, Let A = {x}, B = V - {x}.

Step 2: Select (u, v)  E, u  A, v  B such that (u, v) has


the smallest weight between A and B.

Step 3: Put (u, v) into the tree. A = A  {v}, B = B - {v}

Step 4: If B = , stop; otherwise, go to Step 2.


68
AN EXAMPLE FOR PRIM’S ALGORITHM

69
EXAMPLE2

4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d b d d
b 3 3 b 3

c 4 c
4
a a
6 1
6 1
2 2
d b d
b 3 3
70
PRIM’S ALGORITHM - PERFORMANCE

Needs priority queue for locating closest fringe vertex.

Efficiency
O(n2) for weight matrix representation of graph and array
implementation of priority queue
O(m log n) for adjacency lists representation of graph with n vertices
and m edges and min-heap implementation of the priority queue

71

You might also like