Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Abstractions

Similar presentations


Presentation on theme: "Programming Abstractions"— Presentation transcript:

1 Programming Abstractions
CS106B Cynthia Lee

2 Graphs Topics Graphs! Basics What are they? How do we represent them?
Theorems What are some things we can prove about graphs? Breadth-first search on a graph Spoiler: just a very, very small change to tree version Dijkstra’s shortest paths algorithm Spoiler: just a very, very small change to BFS A* shortest paths algorithm Spoiler: just a very, very small change to Dijkstra’s Minimum Spanning Tree Kruskal’s algorithm

3 A A 0? 6 B B 3 C C 3 1 2 1 D D 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

4 A A 0? 6 B B 3 C C A 0? 3 1 2 1 D D 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

5 A A 0? 6 B B 3 C C A 0? 3 1 2 1 D D 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

6 A A 0? 6 B B 3 C C 3 1 2 1 D D 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

7 A A 6 B B 3 C C 3 1 2 1 D D 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

8 A A 6 B B 6? 3 C C 3 1 2 1 D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

9 A A B B 6? C C D 3? B 6? D D 3? E E F F G G H H I I
6 B B 6? 3 C C D 3? 3 1 1 2 B 6? D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

10 A A B B 6? C C D 3? B 6? D D 3? E E F F G G H H I I
6 B B 6? 3 C C D 3? 3 1 1 2 B 6? D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

11 A A B B 6? C C D 3? B 6? D D 3? E E F F G G H H I I
6 B B 6? 3 C C D 3? 3 1 1 2 B 6? D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

12 A A 6 B B 6? 3 C C B 6? 3 1 1 2 D D 3? 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

13 A A 6 B B 6? 3 C C B 6? 3 1 1 2 D D 3 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

14 A A 6 B 6? B 3 C C B 6? 3 1 1 2 D D 3 1 E 4? E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

15 A A 6 B 6? B 3 C C 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

16 A A B B 6? C C E 4? B 6? D D 3 E 4? E F F G 12? G G 12? H H I I
6 B B 6? 3 C C E 4? 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F G 12? 9 4 4 7 G G 12? 2 H H 5 I I

17 A A B B 6? C C E 4? B 6? D D 3 E 4? E F F G 12? G G 12? H H I I
6 B B 6? 3 C C E 4? 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F G 12? 9 4 4 7 G G 12? 2 H H 5 I I

18 A A B B 6? C C E 4? B 6? D D 3 E 4? E F F G 12? G G 12? H H I I
6 B B 6? 3 C C E 4? 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F G 12? 9 4 4 7 G G 12? 2 H H 5 I I

19 A A B B 6? C C B 6? G 12? D D 3 E 4? E F F G G 12? H H I I
6 B B 6? 3 C C B 6? 3 1 1 2 G 12? D D 3 1 E 4? E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

20 A A B B 6? C C B 6? G 12? D D 3 E 4 E F F G G 12? H H I I
6 B B 6? 3 C C B 6? 3 1 1 2 G 12? D D 3 1 E 4 E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

21 A A B 6? B C C B 6? G 12? D D 3 E E 4 F F 11? G G 12? H 8? H I I
6 B 6? B 3 C C B 6? 3 1 1 2 G 12? D D 3 1 E E 4 7 F F 11? 9 4 7 4 G G 12? 2 H 8? H 5 I I

22 A A B 6? B C C B 6? D D 3 E E 4 F F 11? G 12? G G 12? H H 8? I I
6 B 6? B 3 C C B 6? 3 1 1 2 D D 3 1 E E 4 7 F F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

23 A A B 6? B C C B 6? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 6? B 3 C C B 6? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

24 A A B 6? B C C B 6? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 6? B 3 C C B 6? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

25 A A B 5? B C C B 6? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 5? B 3 C C B 6? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

26 A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

27 A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

28 A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

29 A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H
6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

30 A A B 5? B C C H 8? F 11? D D 3 E 4 E F F 11? G 12? G G 12? H H 8? I I
6 B 5? B 3 C C H 8? 3 1 1 2 F 11? D D 3 1 E 4 E 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H H 8? 5 I I

31 A A B 5 B C C H 8? F 11? D D 3 E 4 E F F 11? G 12? G G 12? H H 8? I I
6 B 5 B 3 C C H 8? 3 1 1 2 F 11? D D 3 1 E 4 E 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H H 8? 5 I I

32 A A B 5 B C 8? C H 8? F 11? D D 3 E E 4 F F 11? G 12? G G 12? H H 8? I
6 B 5 B 3 C 8? C H 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H H 8? 5 I I

33 A A B 5 B C 8? C H 8? D D 3 E E 4 F 11? F F 11? G 12? G G 12? H H 8? I
6 B 5 B 3 C 8? C H 8? 3 1 2 1 D D 3 1 E E 4 7 F 11? F F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

34 A A 6 B 5 B 3 C 8? C H 8? 3 1 2 1 C 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

35 A A 6 B 5 B 3 C 8? C H 8? 3 1 2 1 C 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

36 A A 6 B 5 B 3 C 8? C H 8? 3 1 2 1 C 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

37 You predict the next queue state:
H,C,F,G,I C,F,G,I C,G,F,I Other/none/ more A A 6 B 5 B 3 C C 8? H 8? 3 1 1 2 C 8? D D 3 1 E E 4 7 F 11? F F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

38 A A B 5 B C 8? C C 8? F 11? D D 3 E E 4 F F 11? G 12? G G 12? H 8? H I
6 B 5 B 3 C 8? C C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H 8? H 5 I I

39 A A B 5 B C 8? C C 8? F 11? D D 3 E E 4 F F 11? G 12? G G 12? H 8 H I
6 B 5 B 3 C 8? C C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H 8 H 5 I I

40 A A B B 5 C 8? C C 8? F 11? D D 3 E E 4 F 11? F G 12? G G 12? H H 8? H
6 B B 5 3 C 8? C C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? 9 4 4 7 G G 12? 2 H H 8? H 8 5 I 13? I

41 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? I 13? 9 4 4 7 G G 12? 2 H H 8? H 8 5 I I 13?

42 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? I 13? 9 4 4 7 G G 12? 2 H H 8? H 8 5 I I 13?

43 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

44 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 10? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

45 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 10? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

46 A A 6 B B 5 3 C 8? C C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? I 13? 9 4 7 4 G G 10? 2 H 8 H 8? H 5 I I 13?

47 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

48 A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

49 A A 6 B B 5 3 C 8? C C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

50 A A 6 B B 5 3 C C 8? C 8? 3 1 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

51 A A B B 5 C C 8? G 10? F 11? D D 3 E E 4 F 11? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8? G 10? 3 1 1 F 11? D D 3 1 E E 4 7 F 11? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

52 A A B B 5 C C 8 G 10? F 11? D D 3 E E 4 F 11? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 G 10? 3 1 1 F 11? D D 3 1 E E 4 7 F 11? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

53 A A B B 5 C C 8 G 10? F 11? D D 3 E E 4 F 11? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 G 10? 3 1 1 F 11? D D 3 1 E E 4 7 F 11? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

54 A A B B 5 C C 8 G 10? F 9? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 G 10? 3 1 1 F 9? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

55 A A B B 5 C C 8 G 10? F 9? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 G 10? 3 1 1 F 9? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

56 A A B B 5 C C 8 F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 F 9? 3 1 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

57 A A B B 5 C C 8 F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 F 9? 3 1 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

58 A A B B 5 C C 8 F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C C 8 F 9? 3 1 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

59 A A B B 5 C 8 C F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C 8 C F 9? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

60 A A B B 5 C 8 C F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I
6 B B 5 3 C 8 C F 9? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

61 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9? F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9? F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

62 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

63 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

64 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

65 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

66 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

67 A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13?
6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

68 A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

69 A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10 2 H H 8 5 I 13? I

70 A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10 2 H H 8 5 I 13? I

71 A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10 2 H H 8 5 I 13? I

72 A A 6 B 5 B 3 C 8 C 3 1 2 1 D D 3 1 E E 4 7 F 9 F 9 4 4 7 G G 10 2 H 8 H 5 I 13? I

73 A A 6 B 5 B 3 C 8 C 3 1 2 1 D D 3 1 E E 4 7 F 9 F 9 4 4 7 G G 10 2 H 8 H 5 I 13 I

74 A A 6 B 5 B 3 C 8 C 3 1 2 1 D D 3 1 E E 4 7 F 9 F 9 4 4 7 G G 10 2 H 8 H 5 I 13 I

75 Dijkstra's Algorithm Split nodes apart into three groups:
Green nodes, where we already have the shortest path; Gray nodes, which we have never seen; and Yellow nodes that we still need to process. Dijkstra's algorithm works as follows: Mark all nodes gray except the start node, which is yellow and has cost 0. Until no yellow nodes remain: Choose the yellow node with the lowest total cost. Mark that node green. Mark all its gray neighbors yellow and with the appropriate cost. Update the costs of all adjacent yellow nodes by considering the path through the current node.

76 An Important Note The version of Dijkstra's algorithm I have just described is not the same as the version described in the course reader. This version is more complex than the book's version, but is much faster. THIS IS THE VERSION YOU MUST USE ON YOUR TRAILBLAZER ASSIGNMENT!

77 How Dijkstra's Works Dijkstra's algorithm works by incrementally computing the shortest path to intermediary nodes in the graph in case they prove to be useful. Most of these nodes are completely in the wrong direction. No “big-picture” conception of how to get to the destination – the algorithm explores outward in all directions. Could we give the algorithm a hint?

78 Dijkstra’s: SPIN analysis (shoutout to GSB students)
Situation: Dijkstra's algorithm works by incrementally computing the shortest path to intermediary nodes in the graph in case they prove to be useful. Problem: No big-picture conception of how to get to the destination – the algorithm explores outward in all directions, “in case.” Implication: Most of these explored nodes will end up being in completely the wrong direction. Need: Could we give the algorithm a “hint” of which direction to go?

79 A* and Dijkstra’s Close cousins

80 predicted-distance ≤ actual-distance
Heuristics In the context of graph searches, a heuristic function is a function that guesses the distance from some known node to the destination node. The guess doesn't have to be correct, but it should try to be as accurate as possible. Examples: For Google Maps, a heuristic for estimating distance might be the straight-line “as the crow flies” distance. Admissible Heuristics A heuristic function is called an admissible heuristic if it never overestimates the distance from any node to the destination. In other words: predicted-distance ≤ actual-distance

81 priority(u) = distance(s, u) + heuristic(u, t)
Why Heuristics Matter We can modify Dijkstra's algorithm by introducing heuristic functions. Given any node u, there are two associated costs: The actual distance from the start node s. The heuristic distance from u to the end node t. Key idea: Run Dijkstra's algorithm, but use the following priority in the priority queue: priority(u) = distance(s, u) + heuristic(u, t) This modification of Dijkstra's algorithm is called the A* search algorithm. s u t

82 Focuses work in areas likely to be productive.
A* Search As long as the heuristic is admissible (and satisfies one other technical condition), A* will always find the shortest path from the source to the destination node. Can be dramatically faster than Dijkstra's algorithm. Focuses work in areas likely to be productive. Avoids solutions that appear worse until there is evidence they may be appropriate.

83 Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority 0. While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L. If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L. Dijkstra's Algorithm

84 Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority h(s,t). While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L + h(v,t). If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L + h(v,t). A* Search

85 A* on two points where the heuristic is slightly misleading due to a wall blocking the way

86 A* starts with start node yellow, other nodes grey.

87 A*: dequeue start node, turns green.

88 A*: enqueue neighbors with candidate distance + heuristic distance as the priority value.
1 + 6? 1 + 6? 1 + 4? 1 + 6?

89 A*: dequeue min-priority-value node.
1 + 6? 1 + 6? 1 1 + 6?

90 What goes in the ? 2 + 5? 1 + 6? 2 + 4? Other/none/more ??? 1 + 6? 2 +

91 A*: enqueue neighbors. 1 + 6? 2 + 5? 1 + 6? 1 2 + 3? 1 + 6? 2 + 5?

92 1 + 6? 2 + 5? Now we’re done with the green “1” node’s turn. What is the next node to turn green? (and what would it be if this were Dijkstra’s?) 1 + 6? 1 2 + 3? 1 + 6? 2 + 5?

93 A. : dequeue next lowest priority value node
A*: dequeue next lowest priority value node. Notice we are making a straight line right for the end point, not wasting time with other directions. 1 + 6? 2 + 5? 1 + 6? 1 2 1 + 6? 2 + 5?

94 A*: enqueue neighbors—uh-oh, wall blocks us from continuing forward.
1 + 6? 2 + 5? 3 + 4? 1 + 6? 1 2 1 + 6? 2 + 5? 3 + 4?

95 A*: eventually figures out how to go around the wall, with some waste in each direction.
3 + 8? 4 + 7? 5 + 6? 6 + 5? 7 + 4? 3 + 8? 2 3 4 5 6 7 + 2? 3 + 8? 2 1 2 3 7 + 2? 3 + 8? 2 1 1 2 8 3 + 8? 2 1 2 3 7 8 + 1? 3 + 8? 2 3 4 5 6 7 8 + 3? 3 + 8? 4 + 7? 5 + 6? 6 + 5? 7 + 4? 8 + 3?

96 For Comparison: What Dijkstra's Algorithm Would Have Searched
8 7 6 5 4 5 6 7 8 9? 7 6 5 4 3 4 5 6 7 8 9? 6 5 4 3 2 3 4 5 6 7 8 9? 5 4 3 2 1 2 3 7 8 9? 4 3 2 1 1 2 8 5 4 3 2 1 2 3 7 8 9? 6 5 4 4 3 2 3 4 5 6 7 8 9? 7 6 5 4 3 4 5 6 7 8 9? 8 7 6 5 4 5 6 7 8 9?

97 Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority 0. While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L. If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L. Dijkstra's Algorithm

98 Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority h(s,t). While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L + h(v,t). If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L + h(v,t). A* Search

99 Minimum Spanning Tree

100 A spanning tree in an undirected graph is a set of edges with no cycles that connects all nodes.
A minimum spanning tree (or MST) is a spanning tree with the least total cost.

101 How many distinct minimum spanning trees are in this graph?
B D F 1 7 3 3 Edges: (A,B)=1 (A,C)=3 (B,C)=6 (B,D)=3 (C,E)=3 (D,E)=3 (D,F)=7 A C E 3 3 How many distinct minimum spanning trees are in this graph? 0-1 6-7 2-3 >7 4-5

102 Kruskal’s algorithm Remove all edges from graph
Place all edges in a PQ based on length/weight While !PQ.isEmpty(): Dequeue edge If the edge connects previous disconnected nodes or groups of nodes, keep the edge Otherwise discard the edge

103 Kruskal’s algorithm

104 The Good Will Hunting Problem

105 Video Clip

106 “Draw all the homeomorphically irreducible trees with n=10.”

107 “Draw all the homeomorphically irreducible trees with n=10.”
In this case “trees” simply means graphs with no cycles “with n = 10” (i.e., has 10 nodes) “homeomorphically irreducible” No nodes of degree 2 allowed in your solutions For this problem, nodes of degree 2 are useless in terms of tree structure—they just act as a blip on an edge—and are therefore banned Have to be actually different Ignore superficial changes in rotation or angles of drawing


Download ppt "Programming Abstractions"

Similar presentations


Ads by Google