Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey.

Similar presentations


Presentation on theme: "CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey."— Presentation transcript:

1 CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.Cynthia Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

2 Graphs What are graphs? What are they good for?

3 Graph This file is licensed under the Creative Commons Attribution 3.0 Unported license. Jfd34 http://commons.wikimedia.org/wiki/File:Ryan_ten_Doeschate_ODI_batting_graph.svgCreative CommonsAttribution 3.0 Unported Jfd34http://commons.wikimedia.org/wiki/File:Ryan_ten_Doeschate_ODI_batting_graph.svg

4 A Social Network Slide by Keith Schwarz

5 Chemical Bonds http://4.bp.blogspot.com/-xCtBJ8lKHqA/Tjm0BONWBRI/AAAAAAAAAK4/-mHrbAUOHHg/s1600/Ethanol2.gif Slide by Keith Schwarz

6 http://strangemaps.files.wordpress.com/2007/02/fullinterstatemap-web.jpg Slide by Keith Schwarz

7 Internet 7 This file is licensed under the Creative Commons Attribution 2.5 Generic license. The Opte Project http://commons.wikimedia.org/wiki/File:Internet_map_1024.jpgCreative CommonsAttribution 2.5 Generic The Opte Projecthttp://commons.wikimedia.org/wiki/File:Internet_map_1024.jpg

8 A graph is a mathematical structure for representing relationships  Consists of:  A set V of vertices (or nodes)  Often have an associated label  A set E of edges (or arcs)  Consist of two endpoint vertices  May be directed (an edge from A to B only allow you to go from A to B, not B to A) or undirected (an edge between A and B allows travel in both directions)  Often have an associated cost or weight

9 Graphs How do we represent graphs in code?

10 Diagram shows a graph with four vertices: Apple (outgoing edges to banana and blum) Banana (with outgoing edge to self) Pear (with outgoing edges to banana and plum) Plum (with outgoing edge to banana) Graph terminology  This is a DIRECTED graph  This is an UNDIRECTED graph 10

11 Diagrams each show a graph with four vertices: Apple, Banana, Pear, Plum. Each answer choice has different edge sets. A: no edges B: Apple to Plum directed, Apple to banana undirected C: Apple and Banana point to each other. Two edges point from Plum to Pear Graph terminology  Which of the following is a correct graph? 11 A.B.C. D. None of the above/other/more than one of the above

12 011000 101110 110101 011011 010101 001110 Representing Graphs: Adjacency Matrix We can represent a graph as a Grid (unweighted) or a Grid (weighted) We can represent a graph as a Grid (unweighted) or a Grid (weighted)

13 Representing Graphs: adjacency list NodeConnected To Map > We can represent a graph as a map from nodes to the set of nodes each node is connected to. Slide by Keith Schwarz

14 Common ways of representing graphs  Adjacency list:  Map >  Adjacency matrix:  Grid unweighted  Grid weighted  How many of the following are true?  Adjacency list can be used for directed graphs  Adjacency list can be used for undirected graphs  Adjacency matrix can be used for directed graphs  Adjacency matrix can be used for undirected graphs (A) 0 (B) 1 (C) 2 (D) 3 (E) 4

15 Graphs Theorems about graphs

16 Graphs lend themselves to fun theorems and proofs of said theorems!  Any graph with 6 vertices contains either a triangle (3 vertices with all pairs having an edge) or an empty triangle (3 vertices no two pairs having an edge) 16

17 Diagram shows a graph with four vertices, which are all fully connected with each other by undirected edges (6 edges in total). Eulerian graphs  Let G be an undirected graph  A graph is Eulerian if it can drawn without lifting the pen and without repeating edges  Is this graph Eulerian? A. Yes B. No 17

18 Diagram shows a graph with five vertices, four of which are all fully connected with each other by undirected edges (6 edges in total). The 5 th vertex is connected to two of the remaining four vertices by an edge. (6 + 2 = 8 edges in total) Eulerian graphs  Let G be an undirected graph  A graph is Eulerian if it can drawn without lifting the pen and without repeating edges  What about this graph A. Yes B. No 18

19 Our second graph theorem  Definition: Degree of a vertex: number of edges adjacent to it  Euler’s theorem: a graph is Eulerian iff the number of vertices with odd degrees is either 0 or 2 (eg all vertices or all but two have even degrees)  Does it work for and ? 19

20 Proving Euler’s theorem The following slides geek out on theoretical computer science with a proof of the Eulerian graph theorem. If you like this, YOU WILL LOVE CS103!!

21 Proving Euler’s theorem  Euler’s theorem gives a necessary and sufficient condition for a graph to be Eulerian  All degrees are even  Two degrees odd, rest are even  Will prove in class that this is necessary  Take-home challenge: prove that this is also sufficient 21

22 Proving Euler’s theorem: necessary part  Euler’s theorem (necessary part): If a graph G is Eulerian then all degrees are even; or two degrees are odd and rest are even Try to prove it first yourself 22

23 Proving Euler’s theorem: necessary part  Proof of Euler’s theorem (necessary part): Let G be a graph with an Euler path: v 1,v 2,v 3,….,v k where (v i,v i+1 ) are edges in G; vertices may appear more than once; and each edge of G is accounted for exactly once. The degree of a vertex of G is the number of edges it has. For any internal vertex in the path (eg not v 1 or v k ), we count 2 edges in the path (one going in and one going out). So, any vertex which is not v 1 or v k must have an even degree. If v1  vk then both have odd degrees. If v1=vk is the same vertex this is also has even degree. QED. 23

24 Breadth-First Search Graph algorithms

25 Breadth-First Search AB EF CD GH IJ L K

26

27 AB EF CD GH IJ L K AB EF CD GH IJ L K TO START: (1)Color all nodes GREY (2)Queue is empty

28 F AB EF CD GH IJ L K AB E CD GH IJ L K Breadth-First Search F TO START (2): (1)Enqueue the desired start node (2)Note that anytime we enqueue a node, we mark it YELLOW

29 F AB EF CD GH IJ L K AB E CD GH IJ L K Breadth-First Search F LOOP PROCEDURE: (1)Dequeue a node (2)Mark current node GREEN (3)Set current node’s GREY neighbors’ parent pointers to current node, then enqueue them (remember: set them YELLOW)

30 F AB EF CD GH IJ L K AB E CD GH IJ L K Breadth-First Search F

31 AB E D K F AB EF CD GH IJ L K C GH IJ L F

32 AB E D K F AB EF CD GH IJ L K C GH IJ L ABDEK F

33 AB E D K F AB EF CD GH IJ L K C GH IJ L BDEK A

34 AB E D K F AB EF CD GH IJ L K C GH IJ L BDEK A

35 AB E D K F AB EF CD GH IJ L K C GH IJ L BDEK A

36 AB E D K F AB EF CD GH IJ L K C GH IJ L DEK B

37 AB E D K F AB EF CD GH IJ L K C GH IJ L DEK B

38 H CAB E D K F AB EF CD GH IJ L K G IJ L DEK B CH

39 H CAB E D K F AB EF CD GH IJ L K G IJ L EKCH D

40 H CAB E D K F AB EF CD GH IJ L K G IJ L EKCH D

41 H CAB E D K F AB EF CD GH IJ L K G IJ L EKCH D

42 H CAB E D K F AB EF CD GH IJ L K G IJ L KCH E

43 H CAB E D K F AB EF CD GH IJ L K G IJ L KCH E

44 I H CAB E D K F AB EF CD GH IJ L K G J L KCH E I

45 I H CAB E D K F AB EF CD GH IJ L K G J L CHI K

46 I H CAB E D K F AB EF CD GH IJ L K G J L CHI K

47 I H CAB E D K F AB EF CD GH IJ L K G J L CHI K You predict the next slide! A.K’s neighbors F,G,H are yellow and enqueued and their parents are pointing to K B.K’s neighbors G,H are yellow and enqueued and their parents are pointing to K C.Other/none/more

48 G I H CAB E D K F AB EF CD GH IJ L K J L Breadth-First Search CHI K G

49 G I H CAB E D K F AB EF CD GH IJ L K J L CHI K G

50 G I H CAB E D K F AB EF CD GH IJ L K J L HIG C

51 G I H CAB E D K F AB EF CD GH IJ L K J L HIG C

52 G I H CAB E D K F AB EF CD GH IJ L K J L IG H

53 G I H CAB E D K F AB EF CD GH IJ L K J L IG H

54 G I H CAB E D K F AB EF CD GH IJ L K J L IG

55 G I H CAB E D K F AB EF CD GH IJ L K J L G I

56 G I H CAB E D K F AB EF CD GH IJ L K J L G I

57 L G I H CAB E D K F AB EF CD GH IJ L K J G I L

58 L G I H CAB E D K F AB EF CD GH IJ L K J L G

59 L G I H CAB E D K F AB EF CD GH IJ L K J L G

60 L G I H CAB E D K F AB EF CD GH IJ L K J L

61 L G I H CAB E D K F AB EF CD GH IJ L K J L

62 J L G I H CAB E D K F AB EF CD GH IJ L K L J

63 J L G I H CAB E D K F AB EF CD GH IJ L K J

64 J L G I H CAB E D K F AB EF CD GH IJ L K J

65 Quick question about efficiency…  Let’s say that you have a very geographically diverse extended family— somebody in every city in the western U.S.

66 Quick question about efficiency…  You’re all going to fly to Yosemite and then drive home, and you’ve been tasked with making custom driving directions for everyone

67 Quick question about efficiency…  You calculated the shortest path for yourself (Yosemite to Palo Alto) and let’s just say that it took time X = O((|E| + |V|)log|V|)  With respect to the number of cities |V|, and the number of road segments |E|  How long will it take you, in total, to calculate the shortest path for ALL of your relatives? A. O(|V|*X) B. O(|E|*|V|* X) C. X D. Other/none/more

68 J L G I H CAB E D K F AB EF CD GH IJ L K Breadth-First Search THINGS TO NOTICE: (1)We used a queue (2)What’s left is a kind of subset of the edges, in the form of ‘parent’ pointers (3)If you follow the parent pointers from the desired endpoint, you will get back to the start point, and it will be the shortest way to do that.

69 J L G I H CAB E D K F AB EF CD GH IJ L K Breadth-First Search THINGS TO NOTICE: (4) We now have the answer to the question “What is the shortest path to you from F?” for every single node in the graph!!

70 But wait!!  What you calculated will find the shortest path in terms of fewest number of road segments but doesn’t at all take into account how long each road segment is! 

71 But wait!!  This might be useful if you are a fugitive and want to go from one city to another passing through the fewest other cities on the way…  But what if we want to save gas/fewest miles?


Download ppt "CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey."

Similar presentations


Ads by Google