Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS202 - Fundamental Structures of Computer Science II
CHAPTER 13 Graphs DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
IS 2610: Data Structures Graph April 5, 2004.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
CS 200 Algorithms and Data Structures
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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 Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1) Find and label the degree of each vertex in the graph.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Chapter 20.
Graphs CS Data Structures Mehmet H Gunes
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 11 Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Contents Terminology Graphs as ADTs Applications of Graphs Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Terminology Definition: A set of points that are joined by lines Graphs also represent the relationships among data items G = { V , E }; that is, a graph is a set of vertices and edges A subgraph consists of a subset of a graph’s vertices and a subset of its edges Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-1 An ordinary line graph Terminology FIGURE 20-1 An ordinary line graph Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-2 (a) A campus map as a graph; (b) a subgraph Terminology FIGURE 20-2 (a) A campus map as a graph; (b) a subgraph Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Terminology FIGURE 20-3 Graphs that are (a) connected; (b) disconnected; and (c) complete Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Terminology FIGURE 20-4 (a) A multigraph is not a graph; (b) a self edge is not allowed in a graph Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Terminology Simple path: passes through vertex only once Cycle: a path that begins and ends at same vertex Simple cycle: cycle that does not pass through other vertices more than once Connected graph: each pair of distinct vertices has a path between them Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Terminology Complete graph: each pair of distinct vertices has an edge between them Graph cannot have duplicate edges between vertices Multigraph: does allow multiple edges When labels represent numeric values, graph is called a weighted graph Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Terminology Undirected graphs: edges do not indicate a direction Directed graph, or digraph: each edge has a direction Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-5 (a) A weighted graph; (b) a directed graph Terminology FIGURE 20-5 (a) A weighted graph; (b) a directed graph Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Graphs as ADTs ADT graph operations Test whether graph is empty. Get number of vertices in a graph. Get number of edges in a graph. See whether edge exists between two given vertices. Insert vertex in graph whose vertices have distinct values that differ from new vertex’s value. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Graphs as ADTs ADT graph operations, ctd. Insert edge between two given vertices in graph. Remove specified vertex from graph and any edges between the vertex and other vertices. Remove edge between two vertices in graph. Retrieve from graph vertex that contains given value. View interface for undirected, connected graphs, Listing 20-1 .htm code listing files must be in the same folder as the .ppt files for these links to work Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-6 (a) A directed graph and (b) its adjacency matrix Implementing Graphs FIGURE 20-6 (a) A directed graph and (b) its adjacency matrix Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Implementing Graphs FIGURE 20-7 (a) A weighted undirected graph and (b) its adjacency matrix Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-8 (a) A directed graph and (b) its adjacency list Implementing Graphs FIGURE 20-8 (a) A directed graph and (b) its adjacency list Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-9 (a) A weighted undirected graph and (b) its adjacency list Implementing Graphs FIGURE 20-9 (a) A weighted undirected graph and (b) its adjacency list Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Graph Traversals Visits all of the vertices that it can reach Happens if and only if graph is connected Connected component is subset of vertices visited during traversal that begins at given vertex Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Depth-First Search Goes as far as possible from a vertex before backing up Recursive algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Depth-First Search Iterative algorithm, using a stack Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Depth-First Search Iterative algorithm, using a stack, ctd. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Depth-First Search FIGURE 20-10 Visitation order for (a) a depth-first search; (b) a breadth-first search Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-11 A connected graph with cycles Depth-First Search FIGURE 20-11 A connected graph with cycles Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Depth-First Search FIGURE 20-12 The results of a depth-first traversal, beginning at vertex a , of the graph in Figure 20-11 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Breadth-First Search Visits all vertices adjacent to vertex before going forward See Figure 20-10b Breadth-first search uses a queue Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Breadth-First Search FIGURE 20-13 The results of a breadth-fi rst traversal, beginning at vertex a, of the graph in Figure 20-11 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs Topological Sorting FIGURE 20-14 A directed graph without cycles Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs FIGURE 20-15 The graph in Figure 20-14 arranged according to the topological orders (a) a, g, d, b, e, c, f and (b) a, b, g, d, e, f, c Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs Topological sorting algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs FIGURE 20-16 A trace of topSort1 for the graph in Figure 20-14 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs FIGURE 20-16 A trace of topSort1 for the graph in Figure 20-14 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs FIGURE 20-16 A trace of topSort1 for the graph in Figure 20-14 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs FIGURE 20-16 A trace of topSort1 for the graph in Figure 20-14 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Applications of Graphs FIGURE 20-17 A trace of topSort2 for the graph in Figure 20-14 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Spanning Trees Tree: an undirected connected graph without cycles Observations about undirected graphs Connected undirected graph with n vertices must have at least n – 1 edges. Connected undirected graph with n vertices, exactly n – 1 edges cannot contain a cycle A connected undirected graph with n vertices, more than n – 1 edges must contain at least one cycle Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Spanning Trees FIGURE 20-20 The DFS spanning tree rooted at vertex a for the graph in Figure 20-11 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Spanning Trees DFS spanning tree algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Spanning Trees BFS spanning tree algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Spanning Trees FIGURE 20-21 The BFS spanning tree rooted at vertex a for the graph in Figure 20-11 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Minimum Spanning Trees FIGURE 20-22 A weighted, connected, undirected graph Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Minimum Spanning Trees Minimum spanning tree algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Minimum Spanning Trees FIGURE 20-23 A trace of primsAlgorithm for the graph in Figure 20-22 , beginning at vertex a Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Minimum Spanning Trees FIGURE 20-23 A trace of primsAlgorithm for the graph in Figure 20-22 , beginning at vertex a Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Minimum Spanning Trees FIGURE 20-23 A trace of primsAlgorithm for the graph in Figure 20-22 , beginning at vertex a Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths Shortest path between two vertices in a weighted graph has smallest edge-weight sum FIGURE 20-24 (a) A weighted directed graph and (b) its adjacency matrix Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths Dijkstra’s shortest-path algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths FIGURE 20-25 A trace of the shortest-path algorithm applied to the graph in Figure 20-24 a Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths FIGURE 20-26 Checking weight [u] by examining the graph: (a) weight [2] in step 2; (b) weight [1] in step 3; Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths FIGURE 20-26 Checking weight [u] by examining the graph(c) weight [3] in step 3; (d) weight [3] in step 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths Dijkstra’s shortest-path algorithm, ctd. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths FIGURE 20-25 A trace of the shortest-path algorithm applied to the graph in Figure 20-24 a Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths FIGURE 20-26 Checking weight [u] by examining the graph: (a) weight [2] in step 2; (b) weight [1] in step 3; Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Shortest Paths FIGURE 20-26 Checking weight [u] by examining the graph: (c) weight [3] in step 3; (d) weight [3] in step 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Circuits Another name for a type of cycle common in statement of certain problems Circuits either visit every vertex once or visit every edge once An Euler circuit begins at a vertex v, passes through every edge exactly once, and terminates at v Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Circuits FIGURE 20-27 (a) Euler’s bridge problem and (b) its multigraph representation Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

FIGURE 20-28 Pencil and paper drawings Circuits FIGURE 20-28 Pencil and paper drawings Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Circuits FIGURE 20-29 Connected undirected graphs based on the drawings in Figure 20-28 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Circuits FIGURE 20-30 The steps to determine an Euler circuit for the graph in Figure 20-29 b Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Circuits FIGURE 20-30 The steps to determine an Euler circuit for the graph in Figure 20-29 b Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Some Difficult Problems Hamilton circuit Path that begins at a vertex v, passes through every vertex in the graph exactly once, and terminates at v . The traveling salesperson problem Variation of Hamilton circuit Involves a weighted graph that represents a road map Circuit traveled must be the least expensive Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Some Difficult Problems FIGURE 20-31 The three utilities problem Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Some Difficult Problems Planar graph Can draw it in a plane in at least one way so that no two edges cross The four-color problem Given a planar graph, can you color the vertices so that no adjacent vertices have the same color, if you use at most four colors? Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

End Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012