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

08 Graphs

Uploaded by

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

08 Graphs

Uploaded by

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

CPE 354: Data Structures and

Algorithms

Graphs

Dr. Ola Ta’ani

1
Graph Terminology
• vertex, node, point
• edge, line, arc
• G = (V, E)
– V is set of vertices
– E is set of edges
• Each edge joins two vertices

2
Undirected Graph
• Edges do not have a
direction
• The edge from 1 to 2 is
also an edge from 2 to 1
• Edge (1, 2) implies that
there is also an edge (2, 1)
[ The same edge ]

3
Directed Graph
• Edges have a direction
• Edge (2, 1) means only
that there is an edge from
2 to 1
• In this example, there is no
edge from 1 to 2

4
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,
tail
v1> != <v1,v0> head
Weighted Graph
• Weights (values) are
associated with the
edges in the graph
• May be directed or
undirected
• Weighted graphs are
also referred to as
networks

6
Complete Graph

• For each pair of vertices, there is one


edge
• If G = (V, E) is a complete graph, and |V| =
n, then can you calculate |E|?
7
Complete Graph
• For each pair of distinct vertices there is
exactly one edge connecting them

• Number of edges is:

v v! v ( v  1) 2
    O ( v )
 2  ( v  2)!2! 2
 

8
Connectivity
• Let n = #vertices, and m = #edges
• 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 
Subgraph

• Subset of vertices and edges forming a graph


• A subgraph G’ of graph G = (V, E) is a
graph (V’, E’) that V’ V and E’ E.

10
Degree of vertex
• The number of edges incident with v
• Deg(v)=0  isolated vertex

11
Path
• The sequence of edges
a b
(v1, v2), (v2, v3),…,(vk-1,
vk).
c
• Denoted as path v1,
v2, ..., vk d e
• such that consecutive
vertices vi and vi+1 are a
a b b
adjacent.
• Length of path is sum of c c
the lengths of the edges
d e d e
abedc bedc
12
More Terminology
• simple path: no repeated vertices
a b

bec
c

d e
• cycle: simple path, except that the last vertex is the same as
the first vertex
Even More Terminology
•connected graph: any two or more vertices are connected by some path

connected not connected

• connected component: maximal connected subgraph. E.g., the graph below has
3 connected components.
• tree - connected graph without cycles.
unique path between every pair of vertices

• forest - collection of trees tree

tree

forest

tree

tree
Connectivity

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

n5
m3
Size of a Graph
• Graph G = (V,E)
n = |V| = # vertices
m = |E| = # edges
size of G is n+m
1

2 3

4
Representation of Graphs
• Adjacency matrix
• Incidence matrix
• Adjacency lists: Table, Linked List
• Space/time trade-offs depending on which
operation are most frequent as well as
properties of the graph

18
Adjacency Matrix

Let G=(V,E) be a graph with n vertices.


The adjacency matrix of G is a two-dimensiona
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph i
symmetric; the adjacency matrix for a digraph
need not be symmetric
Adjacency Matrix

Undirected graph Symmetric matrix

20
Examples for Adjacency Matrix
4
0 0
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1    7
  1 0 1 
2 0 1 1 0 0 0 0 0
1 1 0 1 0 0 0 1
   0 0 1 0 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2
G1  
0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
 
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
 
0 0 0 0 0 0 1 0
undirected: n2/2
directed: n2
G4
Merits of Adjacency Matrix

• From the adjacency matrix, to determine the


connection of vertices is easy
n 1

• The degree of a vertex is  adj _ mat[i][ j ]


j 0

• For a digraph (directed graph), the row sum is


the out_degree, while the column sum is the
in_degree
n 1 n 1
ind (vi )   A[ j , i ] outd (vi )   A[i, j ]
j 0 j 0
Incidence matrix
• The incidence matrix of G is a p × q matrix (bij),
where p and q are the numbers of vertices and
edges respectively, such that bij = 1 if the vertex
vi and edge xj are incident and 0 otherwise.

23
Adjacency List
• Vertices are stored as records or objects,
and every vertex stores a list of adjacent
vertices
• Linked list {a,b}, {a,c}, {b,c}

• Table

24
NULL
0 0 4

2 1 5
1 2 3 6

3 7

0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Graph Traversal
Can we use tree traversal algorithms to
traverse graphs?

Why?

26
Graph Traversal
• Problem: Search for a certain node or
traverse all nodes in the graph
• 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 search
• Starting from vertex v
• Mark v as marked
• Select u as an unmarked node adjacent to v
• If no u, quit
• If u, begin depth first search from u
• When search from u quits, select another node
from v
• Similar to preorder tree traversal

28
Example
A unexplored vertex A

A visited vertex
B D E
unexplored edge
discovery edge
C
back edge

A A

B D E B D E

C C

Depth-First Search 29
Example (cont.)
A A

B D E B D E

C C

A A

B D E B D E

C C

Depth-First Search 30
Depth-first searching
• A depth-first search (DFS)
A explores a path all the way
to a leaf before backtracking
B C
and exploring another path
• For example, after
searching A, then B, then D,
D E F G
the search backtracks and
tries another path from B
H I J K • Node are explored in the
order A B D E H L M N I O P C
FGJKQ
L M N O P Q
• In this example, N will be
found before J
Breadth first search
• Starting from node v
• Identify all nodes adjacent to v
• Add these to the set
• Determine set of unvisited nodes which
are adjacent to this set
• Add these to the set
• Continue until no new nodes are
encountered
32
Example
L0
unexplored vertex A
A
A visited vertex L1
B C D
unexplored edge
discovery edge E F
cross edge

L0 L0
A A

L1 L1
B C D B C D

E F E F

Breadth-First Search 33
Example (cont.)
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

Breadth-First Search 34
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 F

Breadth-First Search 35
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
Exercise

• What would the visit orders for DFS(1),


DFS(5), BFS(1), BFS(5) look like?

37
Unweighted Shortest Path
• Find the shortest path (measured by
number of edges) from a designated
vertex S to every vertex
• Simplified case of weighted shortest path

38
Algorithm
• Starting from node S
• Distance from S to S is 0, so label as 0
• Find all nodes which are distance 1 from S
• Label as distance 1
• Find all nodes which are distance 2 from S
• These are 1 step from those labeled 1
• This is precisely a breadth first search

39
Positive Weighted Shortest Path
• Length is sum of the edges costs on the
path
• All edges have nonnegative cost
• Find shortest paths from some start vertex
to all vertices
• similar process to unweighted case
• Dijkstra's Algorithm

40
Dijkstra's Algorithm
• Distance at each node v is shortest path
distance from s to v using only known
vertices as intermediates
• An example of a Greedy Algorithm
• Solve problem in stages by doing what
appears to be the best thing at each stage
• Decision in one stage is not changed later

41
Dijkstra's Algorithm – Details
• Maintain two sets of vertices
– S: the set of vertices whose shortest paths
from the source have been already
determined
– V-S: the remaining vertices

42
Dijkstra's Algorithm – Details
• Maintain two data structures
– d: array of best estimates of shortest path to
each vertex
– pi: array of predecessor for each vertex

43
Algorithm Initialization
initialize_single_source( Graph g, Node s )
for each vertex v in Vertices( g )
g.d[v] : infinity
g.pi[v] := nil
g.d[s] := 0;

44
Relaxation
relax( Node u, Node v, double w [][] )
if d[v] > d[u] + w[u,v] then
d[v] := d[u] + w[u,v]
pi[v] := u

45
The Algorithm Steps
• Initialize d and pi,
• Set S to empty,
• While there are still vertices in V-S,
– Sort the vertices in V-S according to the current
best estimate of their distance from the source,
– Add u, the closest vertex in V-S, to S,
– Relax all the vertices still in V-S connected to u

46
Pseudo-code
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in Q */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
47
Dijkstra's Shortest Path
Algorithm
• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44

48
Dijkstra's Shortest Path
S={ }
Algorithm
Q = { s, 2, 3, 4, 5, 6, 7, t }



24
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
49 label  
Dijkstra's Shortest Path
S={ }
Algorithm
Q = { s, 2, 3, 4, 5, 6, 7, t }

delmin


24
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
50 label  
Dijkstra's Shortest Path
S={s}
Algorithm
Q = { 2, 3, 4, 5, 6, 7, t }

decrease key



X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
51 label  15
X 
Dijkstra's Shortest Path
S={s}
Algorithm
Q = { 2, 3, 4, 5, 6, 7, t }

delmin


X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance
52 label  15
X 
Dijkstra's Shortest Path
Algorithm
S = { s, 2 }
Q = { 3, 4, 5, 6, 7, t }



X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

53  15
X 
Dijkstra's Shortest Path
Algorithm
S = { s, 2 }
Q = { 3, 4, 5, 6, 7, t }

decrease key

X 33

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

54  15
X 
Dijkstra's Shortest Path
Algorithm
S = { s, 2 }
Q = { 3, 4, 5, 6, 7, t }

X 33

X 9
24
2 3
0 9
delmin
s
18
14 X 14
 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

55  15
X 
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 6 }
Q = { 3, 4, 5, 7, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

56  15
X 
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 6 }
Q = { 3, 4, 5, 7, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

57  15
X delmin 
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 6, 7 }
Q = { 3, 4, 5, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

58  15
X 59 
X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 6, 7 }
Q = { 3, 4, 5, t } delmin

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

59  15
X 59 
X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 6, 7 }
Q = { 4, 5, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

60  15
X X 
51 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 6, 7 }
Q = { 4, 5, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16
delmin

t
7 44

61  15
X X 
51 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 5, 6, 7 }
Q = { 4, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 45 
X
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

62  15
X 50 51 X 
X 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 5, 6, 7 }
Q = { 4, t }

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 45 
X
X 34
X 35
44
30 X
 4 19
11
15 5 delmin
5
6
20 16

t
7 44

63  15
X 50 51 X 
X 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
Q={t}

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 45 
X
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

64  15
X 50 51 X 
X 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
Q={t}

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 45 
X
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

65  15
X
delmin 50 51 X 
X 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
Q={}

32
X 33
X

X 9
24
2 3
0 9

s
18
14 X 14
 6
2
6 45 
X
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

66  15
X 50 51 X 
X 59 X
Dijkstra's Shortest Path
Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
Q={}

32
X 33
X
X 9

24
2 3
0 9

s
18
14 X 14
 6
2
6 45 
X
X 34
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

67  15
X 50 51 X 
X 59 X
Execution of Dijkstra’s algorithm

2 3 1 2 3 1
1 1
6 6 
5 2 5 2
3 3
1 4 1 4 2
2
3 3
2 2
 4 5 4 5

Iteration N D2 D3 D4 D5 D6
Initial {1} 3 2 5  
1 {1,3} 3 2 4  3
2 {1,2,3} 3 2 4 7 3 
3 {1,2,3,6} 3 2 4  5 3
4 {1,2,3,4,6} 3 2 4 5  3
5 {1,2,3,4,5,6} 3 2 4 5 3
Shortest Paths in Dijkstra’s
Algorithm
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5

2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
Bellman-Ford
• Works with negative-weight edges, but not
negative weight cycles
• Returns false when the graph has
negative-weight cycles

70
Bellman-Ford Algorithm
Bellman-Ford(G,w,s)
01 for each v  V[G]
02 d[v]  
03 d[s]  0
04 [s]  NIL
05 for i  1 to |V[G]|-1 do
06 for each edge (u,v)  E[G] do
07 Relax (u,v,w)
08 for each edge (u,v)  E[G] do
09 if d[v] > d[u] + w(u,v) then return false
10 return true

71
Bellman-Ford Example
t 5 x t 5 x
-2 -2
6   6  
-3 -3
s 8 s  8
 -4 7 -4 7
2 2
7 7
 9   9 
y z y z
t 5 x t 5 x
-2 -2
6   6  
-3 -3
s  8 s  8
-4 7 -4 7
2 2
7 7
 9   9 
y z y z
72
Minimum Spanning Trees
• Problem: Connect a set of nodes by a
network of minimal total length
• Some applications:
– Communication networks
– Circuit design
– Layout of highway systems

UNC Chapel Hill Lin/Foskey/Manocha


Motivation: Minimum
Spanning Trees
• To minimize the length of a connecting
network, it never pays to have cycles
• The resulting connection graph is connected,
undirected, and acyclic, i.e., a free tree
(sometimes called simply a tree)
• This is the minimum spanning tree or MST
problem

UNC Chapel Hill Lin/Foskey/Manocha


Generic MST Algorithm
Generic-MST(G, w)
1 A// Contains edges that belong to a MST
2 while A does not form a spanning tree do
3 Find an edge (u,v) that is safe for A
4 AA{(u,v)}
5 return A

Safe edge – edge that does not destroy A’s property


The algorithm manages a set of edges A maintaining
the following loop invariant
Prior to each iteration, A is a subset of some
minimum spanning tree.
At each step, an edge is determined that can be added
to A without violating this invariant. Such an edge
is called a Safe Edge.
75
Kruskal's Algorithm
• Edge based algorithm
• Add the edges one at a time, in increasing
weight order
• The algorithm maintains A – a forest of trees
An edge is accepted if it connects vertices of
distinct trees
• We need a data structure that maintains a
partition, i.e.,a collection of disjoint sets
– MakeSet(S,x): S  S  {{x}}
– Union(Si,Sj): S  S – {Si,Sj}  {Si  Sj}
– FindSet(S, x): returns unique Si  S, where x  Si

76
Kruskal's Algorithm
• The algorithm adds the cheapest edge
that connects two trees of the forest
MST-Kruskal(G,w)
01 A  
02 for each vertex v  V[G] do
03 Make-Set(v)
04 sort the edges of E by non-decreasing weight w
05 for each edge (u,v)  E, in order by non-
decreasing weight do
06 if Find-Set(u)  Find-Set(v) then
07 A  A  {(u,v)}
08 Union(u,v)
09 return A

77
Kruskal Example

78
Kruskal Example (2)

79
Kruskal Example (3)

80
Kruskal Example (4)

81
Prim’s Algorithm
• Vertex based algorithm
• Grows one tree T, one vertex at a time
• A cloud covering the portion of T already
computed
• Label the vertices v outside the cloud with
key[v] – the minimum weight of an edge
connecting v to a vertex in the cloud,
• key[v] = , if no such edge exists
82
Prim Example

83
Prim Example (2)

84
Prim Example (3)

85

You might also like