You are on page 1of 76

資料結構

GRAPH

H.Y.
1
GRAPH
n Graph representations
n DFS and BFS
n Spanning Tree
n Minimum spanning tree (MST)
Ø Kruskal’s algorithm, Prim’s algorithm, Sollin’s algorithm

n Shortest path Length problem


Ø Single source to other destinations
l Dijkstra’s algorithm
l Bellman-Ford algorithm

Ø All pairs of vertex


l Floyd-Warshall algorithm

n AOV network and topological sort


n AOE network and critical path, critical tasks
n Articulation point, biconnected graph, biconnected component

2
GRAPH REPRESENTATIONS
n Adjacency matrix
n Adjacency Lists

n Adjacency multi-lists

n Incidence matrix
n Index+Array

3
ADJACENCY MATRIX

特性:
1. 無向圖的相鄰矩陣必定是symmetric matrix, 但有向圖不一定
2. 無向圖的相鄰矩陣當中,第I 列(行)的元素加總必定是頂點的degree。有向圖相鄰矩陣當中,第 I 列的元素
加總是頂點 I 的out-degree. 第 I 行於算加總為頂點的in-degree
3. 無向圖中,所有矩陣元素加總為邊數的兩倍。而有向圖是所有元素加總等於邊數
4. 相鄰矩陣比較適合用於dense graph (邊數很多,接近O(N^2) )

4
ADJACENCY LISTS

特性:
1. 無向圖的相鄰矩陣當中,第I 條串列長度(vertex[i]) 必定是頂點的degree。有向圖相鄰串列當中,第 I 條串列的
的長度是頂點 I 的out-degree. 所有串列中 I 的出現次數為頂點的in-degree
2. 無向圖中,所有串列節點總數為邊數的兩倍。而有向圖是所有串列節點總數等於邊數
3. 相鄰串列比較適合用於sparse graph (邊數很少 )

5
比較表 ( V:表頂點數︐E表邊數)
Adjacency Matrix Adjacency Lists
適用的圖型 Dense graph Sparse graph
判斷 edge (i,j) 是否存在 容易方便。Time is O(1) 不方便。Time is O(V+E)
判斷圖型是否connected, 求 Time is O(V^2) Time is O(V+E)
spanning tree, 判斷有無cycle 存在等

6
ADJACENCY MULTILISTS

7
INCIDENCE MATRIX

8
INDEX + ARRAY

vertex 1 2 3 4
Index no 1 4 7 10

Index no 1 2 3 4 5 6 7 8 9 10 11 12
Adjacency 2 3 4 1 3 4 1 2 4 1 2 3
vertex

9
GRAPH TRAVERSAL︓DFS AND BFS
n DFS

DFS (1)=1,2,4,8,5,6,3,7

Note:
1. DFS要注意起點為何
2. DFS order 並不唯一。通常拜訪時,會依據頂點編號順序小的優先走訪

10
n BFS

BFS (1)=1,2,3,4,5,6,7,8

Note:
1. BFS要注意起點為何
2. BFS order 並不唯一。通常拜訪時,會依據頂點編號順序小的優先走訪
3. 如果將binary tree 視為無向圖,則 BFS(root) 即為 binary tree 的level-order traversal
11
比較表
DFS BFS
輔助的資料結構 Stack Queue
時間
相鄰矩陣 O(V^2) O(V^2)
相鄰串列 O(V+E) O(V+E)

12
int main()
DFS USING STACK {
Graph g(5); // Total 5 vertices in
graph
void Graph::DFS(int s)
{ while (!stack.empty()) g.addEdge(1, 0);
g.addEdge(0, 2);
// Initially mark all verices as not visited { g.addEdge(2, 1);
vector<bool> visited(V, false); s = stack.top(); g.addEdge(0, 3);
g.addEdge(1, 4);
stack.pop();
// Create a stack for DFS cout << "Following is Depth First
Traversal\n";
stack<int> stack; if (!visited[s]) g.DFS(0);
{
// Push the current source node. return 0;
stack.push(s); cout << s << " "; }
visited[s] = true; Following is Depth First Traversal 0 3 2 1 4
}

for (auto i = adj[s].begin(); i != adj[s].end(); ++i)


if (!visited[*i])
stack.push(*i);
}
}

13
[ALGORITHM補充] BFS ON UNDIRECTED GRAPH

NOTE: 無向圖,各邊成本相同或無加權值時,要求某點到其他頂點的最短路徑長,則使用BFS 即可求出解答


14
[ALGORITHM補充] DFS ON DIRECTED GRAPH

15
16
TYPES OF EDGES ON DFS OF THE DIRECTED GRAPH

The third case handles the remaining possibility

17
EXAMPLE

18
補充

19
[補充]

20
判斷無向圖是否CONNECTED
n 使用DFS or BFS 追蹤圖型。完後,如果每一個頂點都 visited 過,則為connected. 否則,
unconnected.

Boolean visited[n];

For i=1 to n do
Visited [i]=false;

Boolean connected (graph G, vertex S, int n)


{
DFS(S);
for i=1 to n do
if (! Visited[i]) return False; //unconnected

return True; //connected


}

Time is O (V+E) based on the adjacency lists representation


21
FIND THE CONNECTED COMPONENT IN UNDIRECTED GRAPH

22
DETECT CYCLE IN DIRECTED GRAPH
n 原則:只要有back edge 存在,即有cycle (也就是遇到 gray color 之node)
n 修改一下下列的DFS 即可

23
CODE
bool Graph::DFSUtil(int u, int color[])
bool Graph::isCyclic()
{
{
color[u] = GRAY;
// Initialize color of all vertices as WHITE
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i) int *color = new int[V];
for (int i = 0; i < V; i++)
{
color[i] = WHITE;
int v = *i; // An adjacent of u
// Do a DFS traversal beginning with all
// If there is
if (color[v] == GRAY) // vertices
for (int i = 0; i < V; i++)
return true;
if (color[i] == WHITE)
if (DFSUtil(i, color) == true)
// If v is not processed and there is a back
return true;
// edge in subtree rooted with v
if (color[v] == WHITE && DFSUtil(v, color))
return false;
return true;
}
}

// Mark this vertex as processed


color[u] = BLACK;

return false;
}
24
DETECT CYCLE IN UNDIRECTED GRAPH
原則:只要存在back edge 即有cycle
但是如何判斷是back edge in undirected graph???
That is <U,V> is back edge iff V is visited (gray color) and V is not the parent of U

25
CODE
bool Graph::isCyclic()
bool Graph::isCyclicUtil(int v,
{
bool visited[], int parent)
{
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[v] = true;
visited[i] = false;
list<int>::iterator i;
for (i = adj[v].begin(); i !=
for (int u = 0; u < V; u++)
adj[v].end(); ++i)
{
{
if (!visited[u])
if (!visited[*i])
if (isCyclicUtil(u, visited, -1))
{
return true;
if (isCyclicUtil(*i, visited, v))
}
return true;
return false;
}
}
else if (*i != parent)
return true;
}
return false;
}

26
TOPOLOGICAL SORT ALGORITHM
n A topological sort of a dag G=<V,E> is a linear ordering of all its vertices such that if G
contains an edge <u,v> then u appears before in the ordering. (If the graph contains a
cycle, then no linear ordering is possible.)

Time is O(V+E) based on the


adjacency lists representation

27
EXAMPLE

28
29
STRONGLY CONNECTED COMPONENT
Time is O(V+E) based on the
adjacency lists representation

GT =<V,ET>, where ET ={<u,v>, | <v,u> is belonging to E} That is, ET consists of the edges of G with
their directions reversed.

30
SPANNING TREE
n a spanning tree T of an undirected graph G is a subgraph that is a tree which includes all
of the vertices of G. In general, a graph may have several spanning trees, but a graph that
is not connected will not contain a spanning tree.
n Given G=(V,E) is a connected undirected graph, let S=(V,T) is one of the spanning trees
of G, and S satisfies the following properties:
Ø T is the tree edges of G when applying the graph traversal and the B is the remaining edges.
Hence E=T+B
Ø Take any edge from B and add it to the S, then S will form an unique cycle
Ø In the S, any pair of vertex will have an unique simple path

n Questions
Ø If the undirected graph is unconnected, then will G have spanning tree?
Ø Any connected undirected graph G, there is only one spanning tree in G ?
Ø For a connected undirected graph G, any pair of spanning trees in G will have at least one
common edge?

31
EXAMPLE

Spanning tree 個數>=1 個


取決於DFS or BFS order

32
MINIMUM SPANNING TREE (MST)
n A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the
edges of a connected, edge-weighted undirected graph that connects all
the vertices together, without any cycles and with the minimum possible total edge weight.
That is, it is a spanning tree whose sum of edge weights is as small as possible.
n Applications
Ø 電路佈局成本最小化

Ø 連結n 個城市的最小交通建設成本

Ø 只要是:要連接n 個頂點,需要最少n-1條邊,且這些邊的成本總和最小,皆是此應用

33
MST ALGORITHM
n There are three algorithm
Ø Kruskal’s algorithm

Ø Prim’s algorithm

Ø Sollin’s algorithm

n They adopt the “Greedy” strategy

34
KRUSKAL’S ALGORITHM

• Use the min-heap to maintain the all cost of


edges.So the line 3 delete-min is O(log E),
• Lin5 will cost O(1) time to judge the cycle will be
formed Based on the Disjoint sets Union and Find
operations
• And the number of loop will run at most E times
Hence the Kruskal‘s algorithm is O(ElogE)

35
KRUSKAL’S ALGORITHM [ALGO版]

The running time of Kruskal’s algorithm is O(ElogE). Observing that E<<V^2, we have logE= O(logV), and so
we can restate the running time of Kruskal’s algorithm as O(ElogV)

36
PRIM ALGORITHM [ALGO版]

Minimum edge weight data structure Time complexity (total)


adjacency matrix, O(|V|^2)
binary heap and adjacency list O((|V|+|E|) log |V|)=O(|E|log |V|)}
Fibonacci heap and adjacency list O(|E|+|V|log |V|)
37
EXAMPLE

38
39
如果圖型之所有邊的成本皆不相同︐則MST 必定唯一
n 證明如下
n Assume the contrary, that there are two different MSTs A and B.
n Since A and B differ despite containing the same nodes, there is at least one edge that
belongs to one but not the other.
n Among such edges, let e1 be the one with least weight; this choice is unique because the
edge weights are all distinct. Without loss of generality, assume e1 is in A.
n As B is an MST, {e1} B must contain a cycle C with e1.
n As a tree, A contains no cycles, therefore C must have an edge e2 that is not in A.
n Since e1 was chosen as the unique lowest-weight edge among those belonging to
exactly one of A and B, the weight of e2 must be greater than the weight of e1.
n As e1 and e2 are part of the cycle C, replacing e2 with e1 in B therefore yields a spanning
tree with a smaller weight.
n This contradicts the assumption that B is an MST.

NOTE: 如果圖型有多邊具有相同值,則MST可能不唯一
40
CYCLE THEORY
n For any cycle C in the graph, if the weight of an edge e of C is larger than the individual
weights of all other edges of C, then this edge cannot belong to an MST.
n 證明
Ø 令 T is MST of G, 且假設 cycle C 中之最大成本邊 e is in the T.

Ø 則我們可以從 T 中刪除 e, 且加入 cycle C中之非e 的其他邊,例如 edge W 且 edge W 的成本值必定小於e

Ø 則我們可以得到另一棵 spanning tree S, S 的edges = {edges of T} –e +W

Ø 且 S的邊成本和必定小於 T, 故違反 T is MST 的假設

Ø 所以,T 不會包含 cycle C 中的最大邊

41
CUT THEORY
n For any cut C of the graph, if the weight of an edge e in the cut-set of C is strictly smaller
than the weights of all other edges of the cut-set of C, then this edge belongs to all MSTs
oft he graph.

Proof: Assume that there is an MST T that does not contain e. Adding e to T will produce a cycle, that
crosses the cut once at e and crosses back at another edge e' . Deleting e' we get as panning tree T - {e'}∪{e}
of strictly smaller weight than T. This contradicts the assumption that T was a MST.

42
MIN-COST EDGE THEORY

43
SHORTEST PATH LENGTH PROBLEM

44
比較表
DAG Dijkstra algorithm Bellman-ford Floyd-Warshall Johnson algorithm
(Directed Acyclic Graph) Algorithm algorithm
algorithm
解決問題 Single source to other Single source to Single source to All pairs of vertex All pairs of vertex
destinations other destinations other destinations
策略 Make use of Greedy Dynamic Dynamic Bellman-Ford
“Topological sort” programming programming +Dijkstra
圖型可有 NO YES YES YES YES
cycle
圖型可有 YES NO YES YES YES
negative
cost edge
圖型可有 NO NO NO NO NO
negative
cycle
length
Time O(V+E) O(V2) (相鄰矩陣) O(V3) (相鄰矩陣) O(V3) O(V2logV+VE)

O(ElogV) binary O(VE) (相鄰串列)


heap
O(VlogV+E)
Finonacci Heap
45
SINGLE-SOURCE SHORTEST PATHS IN DIRECTED ACYCLIC GRAPHS(DAG)

Time analysis

46
EXAMPLE

47
DIJKSTRA’S ALGORITHM

48
求5 到其他頂點之最短路徑 Cost matrix(blank is ∞)

49
DIJKSTRA ALGORITHM [ALGO]

50
EXAMPLE

51
TIME ANALYSIS

1. Line 1 Initialize-single-source takes O(V) time


2. Line 3 creates empty priority queue Q takes O(V) time based on all v.d value
3. Line 5 Extract-min(Q) (deletes the min v.d from Q) takes O(logV) time (binary heap or Fibonacci heap used)
And takes V times to empty Q , so the total time is O(VlogV)
4. Line 7 for loop will check all nodes of the adjacency lists, it loop E times
5. Line 8 Relax(u,v,w) is the “decrease key “ operations on binary heap or Fibonacci Heap
So, if binary head is used , it takes O(logV) time to decrease key , hence this total time is O(ElogV)
If the Fibonacci heap is used, it takes O(1) time to decrease key, hence this total time is O(E)

Hence,
If Q is implemented by the binary heap, the Dijkstra algorithm takes O(ElogV) time
If Q is implemented by the Fibonacci heap, the Dijkstra algorithm takes O(VlogV+E) time

52
DIJKSTRA CANNOT APPLY ON THE GRAPH WITH NEGATIVE EDGES

53
(CONT.)

54
BELLMAN-FORD ALGORITHM
n 解single source to other destinations shortest path length
n 允許圖型可以有負邊

n 採用 Dynamic programming 策略

n 令Distk is an array [1..V]


n 其中,Distk[i] = 起點到 i 點 之最短路徑,且途中頂多經過k條edges, 1<=i<=V

n Dist1[1..v]= cost matrix 中 起點那一列元素值,作為初值

n 依序求出 Dist2, Dist3, Dist4,…,Distv-1, 而Distv-1 array 即為結果


n Distk[i] 公式 = min {Distk-1[i], min{Distk-1[j] + cost[j,i], j 是代表有邊射入 i 的頂點} }

55
EXAMPLE: 求頂點S到其他頂點之最短路徑長

K 頂點 s t y x z

1 0 6 7 ∞ ∞

2 0 6 7 4 2

3 0 2 7 4 2

4 0 2 7 4 -2

56
BELLMAN-FORD ALGORITHM
Bellman-Ford (cost, dist, s, n)
// cost: cost matrix n*n, n=頂點數

// dist [1..n] of int


// n :頂點數; s:起點

for i=1 to n do
dist[i]= cost[s,i]
dist[s]=0;

for k=2 to (n-1) do


for each vertex v do

for each u that has edge <u,v> do

if dist[v]> dist[u] +cost[u,v] then


dist[v]= dist[u] +cost[u,v];
}
Time is O(N3) based on the cost matrix (adjacency matrix)

57
BELLMAN-FORD ALGORITHM [ALGO版]

58
FLOYD-WARSHALL ALGORITHM
n Define Ak[i,j] to be the cost of the shortest path from i to j going through no intermediate
vertex of index greater than k.
n Then, An (i,j) will be the cost of the shortest i to j path in G since G contains no vertex with
index greater than n.
n A0(i,j) is just COST(i,j)

n The basic idea in the all pairs algorithm is to successively generate the matrices:
A1,A2, ..An
n 公式

59
SAMPLE

60
FLOYD-WARSHALL ALGORITHM

Time is O(N3)

61
GRAPH WITH NEGATIVE CYCLE
n 所有求最短路徑長的演算法皆無法/解決此一問題

62
[ALGO版本]

Time is O(V3) 63
TRANSITIVE CLOSURE MATRIX AND REFLEXIVE TRANSITIVE CLOSURE MATRIX

A+ A*

64
ALGORITHM FOR A+ AND A*
A* (cost, A, n)
A+ (cost, A, n)
//cost : cost matric n*n
//cost : cost matric n*n
//A: n*n matrix
//A: n*n matrix
//n is the number of vertex
//n is the number of vertex
{
{ for i=1 to n do
for i=1 to n do for j=1 to n do

for j=1 to n do { if i==j then A[i,j]=1;

a[i,j]=cost[i,j]; else a[i,j]=cost[i,j];


}
for k=1 to n do
for k=1 to n do
for i=1 to n do
for i=1 to n do
for j=1 to n do
for j=1 to n do
A[i,j]= A[i,j] or (A[i,k] and A[k,j]);
A[i,j]= A[i,j] or (A[i,k] and A[k,j]);
} }

Time: O(V3)
65
[ALGO]版本

66
AOE NETWORK
CRITICAL TASKS
CRITICAL PATH
AOE (ACTIVITY ON EDGE) NETWORK
Critical path • A1,a4,a7,a10
• A1,a4,a8,a11
Critical path length 18
Critical tasks A1,a4, a7, a8, 10, a11
Bottleneck tasks A1,a4

• Critical path=從start 到 finish 的最長路徑長度=計畫完工之最短天數


• Critical path 可能>=一條
• Critical task=所有critical path上的工作=不可以delay 的工作
• Bottleneck task= 所有 critical paths的交集工作=加速此工作(縮短此工作的天數)可以有效
縮短計畫完工天數

68
event v1 v2 v3 v4 v5 v6 v7 v8 v9

ee 0 6 4 5 7 7 16 14 18

le 0 6 6 8 7 10 16 14 18

ee(i)=start to i 的最長路徑

activity a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11

e最早開工 0 0 0 6 4 5 7 7 7 16 14
時間
L最晚開工 0 2 3 6 6 8 7 7 10 16 14
時間
L-e 0 2 3 0 2 3 0 0 3 0 0

Non-critical a2 a3 a5 a6 a9
tasks
可以delay 2 3 2 3 3
天數

69
ARTICULATION POINT, BICONNECTED GRAPH, BICONNECTED COMPONENT
n Articulation point
Ø A vertex in an undirected connected graph is an articulation point (or cut vertex) if removing it
(and edges through it) disconnects the graph

n Biconnected graph
Ø An undirected connected graph without any articulation points

n Biconnected component
Ø Assume G’ is a biconnected component of G, then G’satisfies the followings
l G’ is biconnected graph

l G’is subgraph of G, and no other subgraph of G (e.g.. Y)can contain G’ and Y is also biconnected.

70
EXAMPLE
• Articulation point={1,3,5,7}
• Biconnected Graph: NO
• Biconnected components={0,1}, {1,2,3,4}, {3,5},{5,6,7}, {7,8},{7,9}

Questions:
1. What‘s the articulation point?
2. List all biconnected components

71
HOW TO FIND THE ARTICULATION POINTS
n 對圖型實施DFS, 求出每個頂點的 dfn (DFS number, 初值可以從0 or 1開始)
n 畫出DFS spanning tree. Also, 標示出back edges

n 求出每個頂點v(or u) 的low 值
Ø 公式 或者

n 判斷頂點x是否是articulation point 規則
Ø 如果 X 是 Root of the DFS spanning tree
l 如果 X 的子點數(degree)>1,則x是articulation point

Ø 如果 X 不是 root
l 針對 X的任一子點 W , if low(W)>=dfn(X) then X是articulation point

72
Draw DFS spanning tree
EXAMPLE

Vertex 0 1 2 3 4 5 6 7 8 9

dfn 4 3 2 0 1 5 6 7 9 8

Low

73
PRACTICE

74
PRACTICE
求出各頂點L(L:LOW)值

Articulation point ={2,3,5} 75


THE END

76

You might also like