Graphs - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Graphs

Description:

Title: 1 Author: lik2 Last modified by: lik Created Date: 1/12/2004 8:00:17 AM Document presentation format: (4:3) – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 48
Provided by: lik2
Category:

less

Transcript and Presenter's Notes

Title: Graphs


1
Graphs
  • 2014, Fall
  • Pusan National University
  • Ki-Joune Li

2
Graph
  • 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

3
Some 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

4
Some 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

5
Some 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

6
Representation 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
7
Representation 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
8
Weighted 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
? ? ? ?
9
Graph Basic Operations
  • Traversal
  • Depth First Search (DFS)
  • Breadth First Search (BFS)
  • Used for search
  • Example
  • Find Yellow Node from 0

10
DFS 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)
11
DFS 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
12
DFS 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
13
DFS 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
14
DFS 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
15
DFS 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
16
BFS 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)
17
Spanning 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
18
Articulation 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
19
Finding 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
20
Finding 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
21
Finding 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

22
Finding 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
23
Finding 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
24
Finding 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
25
Computation 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)
26
Minimum 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
27
Finding 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
28
Kruskals 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
29
Checking Cycles for Kruskals Algorithm
log n
If v ? V and w ? V , then ? cycle,
otherwise no cycle
w
v
30
Prims 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)
31
Finding 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 )
32
Shortest 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
33
Shortest Path Problem
TV
  • Shortest Path
  • Step 1

V-TV
vertex cost
1 5
2 8
3 10
4 ?
5 ?
6 ?
7 ?
34
Shortest Path Problem
TV
  • Shortest Path
  • Step 1

V-TV
vertex cost
1 5
2 8
3 10
4 ?
5 ?
6 ?
7 ?
35
Shortest Path Problem
TV
  • Shortest Path
  • Step 2

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
36
Shortest Path Problem
TV
  • Shortest Path
  • Step 2

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
37
Shortest Path Problem
TV
  • Shortest Path
  • Step 2

vertex cost
1 5
2 8?6
3 10
4 ??8
5 ??7
6 ?
7 ?
6 15 ? 8
6 6 ? 8
V-TV
38
Finding 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 )
39
Finding 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
40
Transitive 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
41
Transitive 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 )
42
Activity Networks
43
AOV 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
44
Topological 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
45
Topological 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
46
Topological 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 )
47
AOE 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
Write a Comment
User Comments (0)
About PowerShow.com