Download presentation

Presentation is loading. Please wait.

Published byAiden Graves Modified over 3 years ago

1

2

3
EXAMPLE: X Y Z T 5 VERTICES 6 EDGES S

4
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet

5

6

7

8
X Y Z T X, Y, T, Z, S HAS LENGTH 4. SHORTEST PATH FROM X to S = ? S IN GENERAL, HOW CAN THE SMALLEST PATH BETWEEN 2 VERTICES BE DETERMINED?

9

10
X Y Z T X, Y, T, Z, X IS A CYCLE. Y, T, S, T, Y IS NOT A CYCLE. S IS Y, Z, T, S, Z, X, Y A CYCLE?

11
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet THIS UNDIRECTED GRAPH IS ACYCLIC.

12

13

14

15

16

17

18

19

20

21
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet

22

23

24

25

26

27

28

29

30

31
x v Y Z T S ASSUME THE VERTICES ARE ENTERED IN ALPHABETICAL ORDER. PERFORM A BREADTH-FIRST ITERATION FROM S.

32

33

34

35

36

37

38

39
X V Y Z T S

40
ASSUME THE EDGES ARE INSERTED INTO THE GRAPH AS FOLLOWS: S, T (SAME AS T, S) S, Z T, Y T, Z V, Y V, Z X, Y X, Z PERFORM A BREADTH-FIRST ITERATION FROM S.

41
vertex_queue vertex where iterator returned by ++ is positioned S T, ZS Z, YT Y, V, XZ V, XY XV X

42
NOTE: 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 FOR A BREADTH-FIRST ITERATION STARTING AT A, THE NEIGHBORS OF A ARE B AND C, BUT NOT D. D NEVER GETS ENQUEUED, AND NEVER GETS RETURNED BY next( ).

43

44
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet

45

46
A STACK!

47

48

49
X V Y Z T S

50
ASSUME THE EDGES ARE INSERTED INTO THE GRAPH AS FOLLOWS: S, T // SAME AS T, S S, Z T, Y T, Z V, Y V, Z X, Y X, Z PERFORM A DEPTH-FIRST ITERATION FROM S.

51
stack (top vertex is leftmost) vertex where iterator returned by ++ is positioned S Z, T S X, V, TZ Y, V, TX V, TY T V T TOP OF STACK IS LEFTMOST.

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google