Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.

Slides:



Advertisements
Similar presentations
Introduction to Graph Theory Instructor: Dr. Chaudhary Department of Computer Science Millersville University Reading Assignment Chapter 1.
Advertisements

Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph & BFS.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
ECE669 L10: Graph Applications March 2, 2004 ECE 669 Parallel Computer Architecture Lecture 10 Graph Applications.
Chapter 11 Graphs and Trees This handout: Terminology of Graphs Eulerian Cycles.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
GRAPH Learning Outcomes Students should be able to:
Chapter 2 Graph Algorithms.
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Chapter 12.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Spring 2007Graphs1 ORD DFW SFO LAX
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COSC 2007 Data Structures II Chapter 14 Graphs I.
CSE 373: Data Structures and Algorithms
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Discrete Maths 11. Graph Theory Objective
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
English for Economic Informatics I Tomáš Foltýnek Theoretical Foundations of Informatics.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs.
Graphs 5/14/ :46 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs 7/18/2018 7:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW Graphs Graphs
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
CMSC 341 Lecture 21 Graphs (Introduction)
Discrete Maths 9. Graphs Objective
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs.
Graphs.
Comp 245 Data Structures Graphs.
Graph Theory.
Graphs Chapter 13.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs CSE 2011 Winter November 2018.
CSE 373: Data Structures and Algorithms
Graph Theory By Amy C. and John M..
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CSE 373 Data Structures and Algorithms
Graphs ORD SFO LAX DFW /15/ :57 AM
Chapter 10 Graphs and Trees
Presentation transcript:

Programming Abstractions Cynthia Lee CS106B

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

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 UnportedJfd34http://commons.wikimedia.org/wiki/File:Ryan_ten_Doeschate_ODI_batting_graph.svg

Graphs in Computer Science Slide by Keith Schwarz A graph is a mathematical structure for representing relationships  A set V of vertices (or nodes)  A set E of edges (or arcs) connecting a pair of vertices

A Social Network Slide by Keith Schwarz

Chemical Bonds Slide by Keith Schwarz

Slide by Keith Schwarz

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

A graph is a mathematical structure for representing relationships  A set V of vertices (or nodes) › Often have an associated label  A set E of edges (or arcs) connecting a pair of vertices › Often have an associated cost or weight  A graph 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)  We talk about the number of vertices or edges as the size of the set, using the set theory notation for size: |V| and |E|

Boggle as a graph Vertex = letter cube; Edge = connection to neighboring cube

Maze as graph If a maze is a graph, what is a vertex and what is an edge?

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 Graphs How many of the following are valid graphs? A)0 B)1 C)2 D)3

Graph Terminology

XU V W Z Y a c b e d f g h Graph terminology: Paths path: A path from vertex a to b is a sequence of edges that can be followed starting from a to reach b.  can be represented as vertices visited, or edges taken  Example: one path from V to Z: {b, h} or {V, X, Z} path length: Number of vertices or edges contained in the path. neighbor or adjacent: Two vertices connected directly by an edge.  example: V and X

Graph terminology: Reachability, connectedness reachable: Vertex a is reachable from b if a path exists from a to b. connected: A graph is connected if every vertex is reachable from every other. complete: If every vertex has a direct edge to every other. a c b d a c b d e

XU V W Z Y a c b e d f g h Graph terminology: Loops and cycles cycle: A path that begins and ends at the same node.  example: {V, X, Y, W, U, V}.  example: {U, W, V, U}.  acyclic graph: One that does not contain any cycles. loop: An edge directly from a node to itself.  Many graphs don't allow loops.

Graph terminology: Weighted graphs weight: Cost associated with a given edge.  Some graphs have weighted edges, and some are unweighted.  Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1, etc.)  Most graphs do not allow negative weights. example: graph of airline flights, weighted by miles between cities: ORD PVD MIA DFW SFO LAX LGA HNL

Representing Graphs Ways we could implement a Graph class

Representing Graphs: Adjacency matrix We can represent a graph as a Grid (unweighted) or Grid (weighted) We can represent a graph as a Grid (unweighted) or 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

Graph Theory Just a little taste of 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)

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?

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?

Our second graph theorem Definition: Degree of a vertex: number of edges adjacent to it Euler’s theorem: a connected 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 ?