1 Chapter 15 Graphs, Trees, and Networks
2 Chap Undirected Graphs 15.2 Directed Graphs 15.3 Trees 15.4 Networks 15.5 Graph Algorithms Iterators Generating a Minimum Spanning Tree Finding the Shortest Path
3 Chap.15 (Cont.) 15.6 Network Class ( 軟體開發三道工序 ): Method Specifications Data Structure Design Method Definitions
4 Graph theory 圖論 [Wiki] In mathematics and computer science, graph theory is the study of graphs, mathematical structures used to model pair-wise relations between objects from a certain collection. ( 研究 某 collection 內 objects 的兩兩關係 )
5 Applications of Graphs 許多實際問題可用 graph 來表示 例如:網址連結的結構,可用有向圖 (directed graph) 頂點 (vertex) 是網頁網址,如果有一個 B 網址的網頁連 結到 A 網址的網頁 ,便有個連結 (edge) 在網址 A,B 之間 類似的 在 travel 問題,生物學,電腦晶片設計,以及 其他許多領域。 Graph 可增加 weight ( 權重 ) 延伸為 weighted graph ( 加權圖 ) 例如 weight 能代表連結 (edge) 的長度
Undirected Graphs
7 Example: X Y Z T ANS: 5 vertices 6 edges S
8 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet
9
10
11
12 X Y Z T X, Y, T, Z, S has length 4. Shortest path from X to S = ? Ans: 2 S In general, how can the smallest path between 2 vertices be determined? We will cover that.
13
14 X Y Z T X, Y, T, Z, X is a cycle. Y, T, S, Z, T, Y is not a cycle. (repeated edges: YT and TY) S Is Y, Z, T, S, Z, X, Y a cycle? Yes!
15 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet This undirected graph is acyclic.
16
17
Directed Graphs
19
20
21
22
23
Trees
25 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet
Networks
27
28
29
30
31
32 Semantic Network A vertex is related to another by some semantics, but not vise versa. For example, Taipei is a city of Taiwan. And, both Taiwan and Japan are countries of Asia. Taipei Taiwan cityOf AsiaJapan countryOf
Graph Algorithms
Iterators
35
36 x v Y Z T S Assume the vertices are entered in alphabetical order. Perform a breadth-first iteration from S. Ans: S, T, Z, Y, V, X
37
38
39
40
41 X V Y Z T S
42 Assume the edges are inserted into the graph as follows: S, T (Same as T, S) S, Z T, Y T, Z V, X V, Z X, Y X, Z Perform a breadth-first iteration from S.
43 queueVertex returned by next( ) S T, ZS Z, YT Y, V, XZ V, XY XV X enqueue dequeue
44
45
46
47 For a breadth-first iteration of a directed graph, the algorithm is the same, but “neighbor” takes into account the direction of the arrow. A B C D Starting at A, the neighbors of A are B and C, but not D. D NEVER gets enqueued, and never gets returned by next( ).
48
49 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet
50
51 A Stack!
52 X V Y Z T S
53 Assume the edges are inserted into the graph as follows: S, T // Same as T, S S, Z T, Y T, Z V, X V, Z X, Y X, Z Perform a depth-first iteration from S.
54 StackVertex returned by next( ) S Z, T S X, V, TZ Y, V, TX V, TY T V T Top Bottom
55
56
57
Generating a Minimum Spanning Tree
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Finding the Shortest Path
78 Shortest Path from A to B C 7 2 A 10 B 1. start with A. check A’s neighbors. save in priority queue (pq): ; 2. remove. check C’s neighbors. we found is shorter than 1. replace B’s weight sum with 9 2. insert to pq 3. make C the predecessor of B now, pq: 3. remove we got B. DONE!
79
80
81
82
83
84
85
86
87
88
89
90
91
A Network Class
93
94
Method Specifications of the Network Class
96
97
98
99
100
101
102
103
104
105
106
A TreeMap This is called “neighborMap”.
keyvalue keyvalue
116 For example, in the outer treeMap key “karen” has the neighborMap below as its value: (mark, 10.0) (w1, weight1) (don, 7.4) (w2, weight2) (courtney, 14.2) (w3, weight3) This neighborMap is by itself another inner treeMap as below: key value Don (7.4) Courtney (14.2) Mark (10.0)