Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 15 Graphs, Trees, and Networks. 2 Chap.15 15.1 Undirected Graphs 15.2 Directed Graphs 15.3 Trees 15.4 Networks 15.5 Graph Algorithms 15.5.1.

Similar presentations


Presentation on theme: "1 Chapter 15 Graphs, Trees, and Networks. 2 Chap.15 15.1 Undirected Graphs 15.2 Directed Graphs 15.3 Trees 15.4 Networks 15.5 Graph Algorithms 15.5.1."— Presentation transcript:

1 1 Chapter 15 Graphs, Trees, and Networks

2 2 Chap.15 15.1 Undirected Graphs 15.2 Directed Graphs 15.3 Trees 15.4 Networks 15.5 Graph Algorithms 15.5.1 Iterators 15.5.3 Generating a Minimum Spanning Tree 15.5.4 Finding the Shortest Path

3 3 Chap.15 (Cont.) 15.6 Network Class ( 軟體開發三道工序 ): 15.6.1 Method Specifications 15.6.2 Data Structure Design 15.6.3 Method Definitions

4 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 5 Applications of Graphs 許多實際問題可用 graph 來表示 例如:網址連結的結構,可用有向圖 (directed graph) 頂點 (vertex) 是網頁網址,如果有一個 B 網址的網頁連 結到 A 網址的網頁 ,便有個連結 (edge) 在網址 A,B 之間 類似的 在 travel 問題,生物學,電腦晶片設計,以及 其他許多領域。 Graph 可增加 weight ( 權重 ) 延伸為 weighted graph ( 加權圖 ) 例如 weight 能代表連結 (edge) 的長度

6 6 15.1 Undirected Graphs

7 7 Example: X Y Z T ANS: 5 vertices 6 edges S

8 8 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet

9 9

10 10

11 11

12 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 13

14 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 15 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet This undirected graph is acyclic.

16 16

17 17

18 18 15.2 Directed Graphs

19 19

20 20

21 21

22 22

23 23

24 24 15.3 Trees

25 25 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet

26 26 15.4 Networks

27 27

28 28

29 29

30 30

31 31

32 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

33 33 15.5 Graph Algorithms

34 34 15.5.1 Iterators

35 35

36 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 37

38 38

39 39

40 40

41 41 X V Y Z T S

42 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 43 queueVertex returned by next( ) S T, ZS Z, YT Y, V, XZ V, XY XV X enqueue dequeue

44 44

45 45

46 46

47 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 48

49 49 Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet

50 50

51 51 A Stack!

52 52 X V Y Z T S

53 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 54 StackVertex returned by next( ) S Z, T S X, V, TZ Y, V, TX V, TY T V T Top Bottom

55 55

56 56

57 57

58 58 15.5.3 Generating a Minimum Spanning Tree

59 59

60 60

61 61

62 62

63 63

64 64

65 65

66 66

67 67

68 68

69 69

70 70

71 71

72 72

73 73

74 74

75 75

76 76

77 77 15.5.4 Finding the Shortest Path

78 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 79

80 80

81 81

82 82

83 83

84 84

85 85

86 86

87 87

88 88

89 89

90 90

91 91

92 92 15.6 A Network Class

93 93

94 94

95 95 15.6.1 Method Specifications of the Network Class

96 96

97 97

98 98

99 99

100 100

101 101

102 102

103 103

104 104

105 105

106 106

107

108

109

110

111 A TreeMap This is called “neighborMap”.

112

113

114 keyvalue keyvalue

115

116 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)

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141


Download ppt "1 Chapter 15 Graphs, Trees, and Networks. 2 Chap.15 15.1 Undirected Graphs 15.2 Directed Graphs 15.3 Trees 15.4 Networks 15.5 Graph Algorithms 15.5.1."

Similar presentations


Ads by Google