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

Slides:



Advertisements
Similar presentations
Chapter 8 Topics in Graph Theory
Advertisements

Introduction to Graph Theory Instructor: Dr. Chaudhary Department of Computer Science Millersville University Reading Assignment Chapter 1.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Introduction to Graphs
1 Lecture 5 (part 2) Graphs II Euler and Hamiltonian Path / Circuit Reading: Epp Chp 11.2, 11.3.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Math Foundations Week 12 Graphs (2). Agenda Paths Connectivity Euler paths Hamilton paths 2.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Chapter 11 Graphs and Trees This handout: Terminology of Graphs Eulerian Cycles.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
GRAPH Learning Outcomes Students should be able to:
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
Chapter 2 Graph Algorithms.
Graph Theory Topics to be covered:
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Representing and Using Graphs
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Graphs, Puzzles, & Map Coloring
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia.
1.5 Graph Theory. Graph Theory The Branch of mathematics in which graphs and networks are used to solve problems.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Programming Abstractions Cynthia Lee CS106X. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Chapter 9: Graphs.
Great Theoretical Ideas in Computer Science for Some.
Chapter 05 Introduction to Graph And Search Algorithms.
COMPSCI 102 Introduction to Discrete Mathematics.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 201: Design and Analysis of Algorithms
Programming Abstractions
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Programming Abstractions
Graphs Representation, BFS, DFS
Graph Theory.
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graph Theory By Amy C. and John M..
Discrete Mathematics for Computer Science
Chapter 10 Graphs and Trees
Presentation transcript:

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 Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Graphs What are graphs? What are they good for?

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

A Social Network Slide by Keith Schwarz

Chemical Bonds Slide by Keith Schwarz

Slide by Keith Schwarz

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

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

Graphs How do we represent graphs in code?

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

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

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)

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

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

Graphs Theorems about graphs

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

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

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

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

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!!

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

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

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

Breadth-First Search Graph algorithms

Breadth-First Search AB EF CD GH IJ L K

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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.

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!!

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! 

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?