Professional Documents
Culture Documents
資結重點3
資結重點3
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
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
}
13
[ALGORITHM補充] BFS ON UNDIRECTED GRAPH
15
16
TYPES OF EDGES ON DFS OF THE DIRECTED GRAPH
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;
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;
}
}
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.)
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
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
34
KRUSKAL’S ALGORITHM
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版]
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.
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)
Time analysis
46
EXAMPLE
47
DIJKSTRA’S ALGORITHM
48
求5 到其他頂點之最短路徑 Cost matrix(blank is ∞)
49
DIJKSTRA ALGORITHM [ALGO]
50
EXAMPLE
51
TIME ANALYSIS
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 策略
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=頂點數
for i=1 to n do
dist[i]= cost[s,i]
dist[s]=0;
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
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
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 的最長路徑
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)值
76