Title: Graphs
1Graphs
- 2014, Fall
- Pusan National University
- Ki-Joune Li
2Graph
- Definition
- G (V,E ) where V a set of vertices and E
ltu ,v gt u ,v ? V a set of edges - Some Properties
- Equivalence of Graphs
- Tree
- a Graph
- Minimal Graph
3Some Terms
- Directed Graph
- G is a directed graph iff ltu,v gt ? ltv,u gt for u
? v ? V - Otherwise G is an undirected graph
- Complete Graph
- Suppose nv number(V )
- Undirected graph G is a complete graph if ne nv
(nv - 1) / 2, where ne is the number of edges - Adjacency
- For a graph G(V,E ) u is adjacent to v iff ? e
ltu,v gt ?E , where u,v ? V - If G is undirected, v is adjacent to u otherwise
v is adjacent from u
4Some Terms
- Subgraph
- G is a subgraph of a graph G iffV (G ) ? V (G
) and E (G ) ? E (G ) - Path from u to v
- A sequence of vertices uv0, v1, v2, vnv ? V ,
such thatltvi ,vi 1gt ? E for every vi - Cycle iff u v
- Connected
- Two vertices u and v are connected iff ? a path
from u to v - Graph G is connected iff for every pair u and v
there is a path from u to v - Connected Components
- A connected subgraph of G
5Some Terms
- Strongly Connected
- An directed graph G is strongly connected iffiff
for every pair u and v there is a path from u to
v and path from v to u - DAG directed acyclic graph (DAG)
- In-degree and Out-Degree
- In-degree of v number of edges coming to v
- Out-degree of v number of edges going from v
6Representation of Graphs Matrix
- Adjacency Matrix
- Ai, j 1, if there is an edge ltvi ,vj gtAi,
j 0, otherwise - Example
- Undirected Graph Symmetric Matrix
- Space complexity O (nv2 ) bits
- In-degree (out-degree) of a node
0 1 1 1
0 0 1 0
0 0 0 1
0 0 0 0
7Representation of Graphs List
- Adjacency List
- Each node has a list of adjacent nodes
- Space Complexity O (nv ne )
- Inverse Adjacent List
0
1
0
1
2
0
0
2
3
8Weighted Graph Network
- For each edge ltvi ,vj gt, a weight is given
- Example Road Network
- Adjacency Matrix
- Ai, j wij, if there is an edge ltvi ,vj gt and
wij is the weight Ai, j ?, otherwise - Adjacency List
? 1.5 2.3 1.2
? ? 1.0 ?
? ? ? 1.9
? ? ? ?
9Graph Basic Operations
- Traversal
- Depth First Search (DFS)
- Breadth First Search (BFS)
- Used for search
- Example
- Find Yellow Node from 0
10DFS Depth First Search
void GraphDFS() visitednew Booleann
for(i0iltni)visitediFALSE DFS(0)
delete visited
0
1
4
2
3
5
void GraphDFS(int v) visitedvTRUE for
each u adjacent to v if(visiteduFALSE)
DFS(u)
6
7
Adjacency List Time Complexity O(e)
Adjacency Matrix Time Complexity O(n2)
11DFS Depth First Search
DFS(0)
void GraphDFS() visitednew Booleann
for(i0iltni)visitediFALSE DFS(0)
delete visited
0
1
4
2
3
5
void GraphDFS(int v) visitedvTRUE for
each u adjacent to v if(visiteduFALSE)
DFS(u)
6
7
12DFS Depth First Search
DFS(1)
void GraphDFS() visitednew Booleann
for(i0iltni)visitediFALSE DFS(0)
delete visited
0
1
4
2
3
5
void GraphDFS(int v) visitedvTRUE for
each u adjacent to v if(visiteduFALSE)
DFS(u)
6
7
13DFS Depth First Search
DFS(4)
void GraphDFS() visitednew Booleann
for(i0iltni)visitediFALSE DFS(0)
delete visited
0
1
4
2
3
5
void GraphDFS(int v) visitedvTRUE for
each u adjacent to v if(visiteduFALSE)
DFS(u)
6
7
14DFS Depth First Search
DFS(5)
void GraphDFS() visitednew Booleann
for(i0iltni)visitediFALSE DFS(0)
delete visited
0
1
4
2
3
5
void GraphDFS(int v) visitedvTRUE for
each u adjacent to v if(visiteduFALSE)
DFS(u)
6
7
15DFS Depth First Search
DFS(7)
void GraphDFS() visitednew Booleann
for(i0iltni)visitediFALSE DFS(0)
delete visited
0
1
4
2
3
5
void GraphDFS(int v) visitedvTRUE for
each u adjacent to v if(visiteduFALSE)
DFS(u)
6
7
16BFS Breadth First Search
void GraphBFS() visitednew Booleann
for(i0iltni)visitediFALSE Queue
queuenew Queue queue.insert(v)
while(queue.empty()!TRUE)
vqueue.delete() for every w adjacent to v)
if(visitedwFALSE)
queue.insert(w) visitedwTRUE
delete visited
0
1
4
2
3
5
6
7
Adjacency List Time Complexity O(e)
Adjacency Matrix Time Complexity O(n2)
17Spanning Tree
- A subgraph T of G (V,E ) is a spanning tree of
G - iff T is tree and V (T )V (G )
- Finding Spanning Tree Traversal
- DFS Spanning Tree
- BFS Spanning Tree
0
1
4
2
3
5
6
7
0
0
1
1
4
4
2
2
3
3
5
5
6
6
7
7
18Articulation Point and Biconnected Components
- Articulation Point
- A vertex v of a graph G is an articulation point
iffthe deletion of v makes G two connected
components - Biconnected Graph
- Connected Graph without articulation point
- Biconnected Components of a graph
0
0
1
1
7
2
6
4
7
1
3
5
6
2
4
2
6
3
5
19Finding Articulation Point DFS Tree
- Back Edge and Cross Edge of DFS Spanning Tree
- Tree edge edge of tree
- Back edge edge to an ancestor
- Cross edge neither tree edge nor back edge
- No cross edge for DFS spanning tree
d
a
Back edge
b
f
b
g
a
c
d
f
c
h
h
e
e
Back edge
g
20Finding Articulation Point
- Articulation point
- Root is an articulation point if it has at least
two children. - u is an articulation point if ? child of u
without back edge to an ancestor of u
d
Back edge
b
f
a
c
h
e
Back edge
g
21Finding Articulation Point by DFS Number
- DFS number of a vertex dfn (v )
- The visit number by DFS
- Low number low (v )
- low (v ) min dfn (v ), mindfn (x ) (v, x
) back edge , minlow (x ) x child of v
- v is an articulation Point
- If v is the root node with at least two children
or - If v has a child u such that low (u ) ? dfn (v )
- v is the boss of subtree if v dies, the subtree
looses the line
22Finding Articulation Point by DFS Number
d
0
b
f
0
5
a
c
2
5
0
h
e
5
0
g
node e has two pathsone along dfs path, and
another via back edge. ? not articulation point
23Finding Articulation Point by DFS Number
d
0
b
f
0
5
a
c
2
5
0
h
node c has two pathsone along dfs path, and
another via a descendant node ? not articulation
point
e
5
0
g
24Finding Articulation Point by DFS Number
d
0
b
f
0
5
a
c
2
5
0
h
node a has only one pathalong dfs path ? its
parent node is an articulation pointsince it
will be disconnected if its parent is deleted.
(node a relies only on its parent.)
e
5
0
g
25Computation of dfs (v ) and low (v )
void GraphDfnLow(x) num1 // num class
variable for(i0iltni) dfnilowi0
DfnLow(x,-1)//x is the root node
void GraphDfnLow(int u,v)
dfnulowunum for(each vertex w adjacent
from u) if(dfnw0) // unvisited w
DfnLow(w,u) lowwmin(lowu,loww)
else if(w!v) lowumin(lowu,dfnw)
//back edge
Adjacency List Time Complexity O(e)
Adjacency Matrix Time Complexity O(n2)
26Minimum Spanning Tree
- G is a weighted graph
- T is the MST of G iff T is a spanning tree of G
and for any other spanning tree of G, C (T ) lt C
(T )
0
7
5
1
7
10
12
3
4
2
6
4
8
2
6
15
3
5
27Finding MST
- Greedy Algorithm
- Choosing a next branch which looks like better
than others. - Not always the optimal solution
- Kruskals Algorithm and Prims Algorithm
- Two greedy algorithms to find MST
Globally optimal solution
Solution by greedy algorithm,only locally
optimal
Current state
28Kruskals Algorithm
Algorithm KruskalMST Input Graph G Output
MST T Begin T ? while( n(T)ltn-1 and G.E
is not empty) (v,w)? smallest edge of
G.E G.E ? G.E-(v,w) if no cycle in
(v,w)?T, T ?(v,w)?T if(n(T)ltn-1)
coutltltNo MST End Algorithm
0
7
X
5
1
7
10
12
X
3
4
T is MST
2
6
4
8
6
15
3
5
Time Complexity O(e loge)
2
29Checking Cycles for Kruskals Algorithm
log n
If v ? V and w ? V , then ? cycle,
otherwise no cycle
w
v
30Prims Algorithm
Algorithm PrimMST Input Graph G Output MST
T Begin Vnew ?v0 Enew ? while(
n(Enew)ltn-1) select v such that (u,v) is
the smallest edge where u? Vnew, v?
Vnew if no such v, break G.E ?
G.E-(u,v) Enew ?(u,v)? Enew Vnew
?v ? Vnew if(n(T)ltn-1) coutltltNo
MST End Algorithm
0
7
5
1
7
10
12
3
2
6
4
8
T is the MST
4
6
15
3
5
2
Time Complexity O( n 2)
31Finding the Edge with Min-Cost
Step 1
Step 2
T
V
TV
TV
0
0
1
1
3
3
2
2
n (n-1) 1 O( n2 )
32Shortest Path Problem
- Shortest Path
- From 0 to all other vertices
vertex cost
1 5
2 6
3 10
4 8
5 7
6 13
7 11
33Shortest Path Problem
TV
V-TV
vertex cost
1 5
2 8
3 10
4 ?
5 ?
6 ?
7 ?
34Shortest Path Problem
TV
V-TV
vertex cost
1 5
2 8
3 10
4 ?
5 ?
6 ?
7 ?
35Shortest Path Problem
TV
V-TV
vertex cost
1 5
2 8?6
3 10
4 ??8
5 ??7
6 ?
7 ?
5 1 ? 8
5 3 ? 8
5 2 ? 8
36Shortest Path Problem
TV
V-TV
vertex cost
1 5
2 8?6
3 10
4 ??8
5 ??7
6 ?
7 ?
5 1 ? 8
5 3 ? 8
5 2 ? 8
37Shortest Path Problem
TV
vertex cost
1 5
2 8?6
3 10
4 ??8
5 ??7
6 ?
7 ?
6 15 ? 8
6 6 ? 8
V-TV
38Finding Shortest Path from Single Source
(Nonnegative Weight)
Algorithm DijkstraShortestPath(G) / G(V,E) /
output Shortest Path Length Array Dn Begin
S ?v0 Dv0?0 for each v in V-v0, do
Dv ? c(v0,v) while S ? V do begin
choose a vertex w in V-S such that Dw is
minimum add w to S for each v in
V-S do Dv ? min(Dv,Dwc(w,v))
end End Algorithm
w
u
v
S
O( n2 )
39Finding Shortest Path from Single Source
(Nonnegative Weight)
1 v0
2 v0, v1
5 v0, v1, v2, v3, v4
4 v0, v1, v2, v4
3 v0, v1, v2
40Transitive Closure
- Transitive Closure
- Set of reachable nodes
0
1
0
1
0
1
2
2
4
2
4
4
3
3
3
A
A
A
41Transitive Closure
Void GraphTransitiveClosure for(int
i0iltni) for (int j0iltnj)
aijcij for(int k0kltnk)
for(int i0iltni) for (int
j0iltnj) aij aij(aik
akj)
O ( n3 )
Void GraphAllPairsShortestPath for(int
i0iltni) for (int j0iltnj)
aijcij for(int k0kltnk)
for(int i0iltni) for (int
j0iltnj) aij min(aij,(aik
akj))
O ( n3 )
42Activity Networks
43AOV Activity-on-vertex
Node 1 is a immediate successor of Node 0
0
Node 5 is a immediate successor of Node 1
1
4
Node 0 ? Node 1
2
Node 1 ? Node 5
3
5
Node 5 is a successor of Node 0
6
7
Node 0 is a predecessor of Node 5
Node 0 ? Node 5
Partial order for some pairs (v, w) (v, w ? V ),
v ? w (but not for all pairs)(cf. Total Order
for every pair (v, w) (v, w ? V ), v ? w or w ?
v )
No Cycle
Transitive and Irreflexive
44Topological Order
0
Ordering (0, 1, 2, 3, 4, 5, 7, 6) Ordering (0,
1, 4, 2, 3, 5, 7, 6)
1
4
2
Both orderings satisfy the partial order
3
5
Topological order Ordering by partial order
6
7
Ordering (0, 1, 2, 5, 4, 3, 7, 6) Not a
topological order
45Topological Ordering
0
0
1
1
4
4
2
2
3
5
3
5
6
7
6
7
0, 1
0, 1, 4
0, 1, 4, 3
4
2
2
3
3
3
5
5
5
6
6
6
7
7
7
46Topological Ordering
Void Topological_Ordering_AOV(Digraph G) for
each node v in G.V if v has no predecessor
cout ltlt v delete v from G.V
deleve (v,w) from G.E if G.V is not
empty, cout ltltCycle found\n
O ( n e )
47AOE Activity-on-edgePERT (Project Evaluation
and Review Technique
Node 2 is completed only if every predecessor is
completed Earliest Time the LONGEST PATH from
node 0? Earliest time of node 1 5? Earliest
time of node 2 8? Earliest time of node 4 8?
Earliest time of node 5 max(811, 52, 86)19?
Earliest time of node 7 max(89, 194)23?
Earliest time of node 6 max(2312, 196)35
0
5
1
3
4
8
1
2
2
11
6
9
5
4
6
7
6
12
0
5
1
3
4
8
1
2
2
11
6
9
5
(0, 1, 4, 5, 7, 6) Critical Path By reducing
the length on the critical path,we can reduce
the length of total path.
4
6
7
6
12