Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice...
-
date post
19-Dec-2015 -
Category
Documents
-
view
218 -
download
0
Transcript of Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice...
![Page 1: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/1.jpg)
Graphs
Chapter 30
Slides by Steve ArmstrongLeTourneau University
Longview, TX2007,Prentice Hall
![Page 2: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/2.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Chapter Contents
• Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees
• Traversals Breadth-First Traversal Dept-First Traversal
![Page 3: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/3.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Chapter Contents
• Topological Order
• Paths Finding a Path Shortest Path in an Unweighted Graph Shortest Path in a Weighted Graph
• Java Interfaces for the ADT Graph
![Page 4: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/4.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Some Examples and Terminology
• Vertices or nodes are connected by edges
• A graph is a collection of distinct vertices and distinct edges Edges can be directed or undirected When it has directed edges it is called a
digraph
• A subgraph is a portion of a graph that itself is a graph
![Page 5: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/5.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Road Maps
Fig. 30-1 A portion of a road map.
NodesNodes
EdgesEdges
![Page 6: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/6.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Road Maps
Fig. 30-2 A directed graph representing a portion of a city's street map.
![Page 7: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/7.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Paths
• A sequence of edges that connect two vertices in a graph
• In a directed graph the direction of the edges must be considered Called a directed path
• A cycle is a path that begins and ends at same vertex Simple path does not pass through any vertex
more than once
• A graph with no cycles is acyclic
![Page 8: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/8.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Weights
• A weighted graph has values on its edges Weights or costs
• A path in a weighted graph also has weight or cost The sum of the edge weights
• Examples of weights Miles between nodes on a map Driving time between nodes Taxi cost between node locations
![Page 9: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/9.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Weights
Fig. 30-3 A weighted graph.
![Page 10: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/10.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Connected Graphs
• A connected graph Has a path between every pair of
distinct vertices
• A complete graph Has an edge between every pair of
distinct vertices
• A disconnected graph Not connected
![Page 11: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/11.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Connected Graphs
Fig. 30-4 Undirected graphs
![Page 12: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/12.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Adjacent Vertices
• Two vertices are adjacent in an undirected graph if they are joined by an edge
• Sometimes adjacent vertices are called neighbors
Fig. 30-5 Vertex A is adjacent to B, but B is not adjacent to A.
![Page 13: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/13.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Airline Routes
• Note the graph with two subgraphs Each subgraph connected Entire graph disconnected
Fig. 30-6 Airline routes
![Page 14: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/14.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Mazes
Fig. 30-7 (a) A maze; (b) its representation as a graph
![Page 15: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/15.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Course Prerequisites
Fig. 30-8 The prerequisite structure for a selection of courses as a directed graph without cycles.
![Page 16: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/16.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Trees• All trees are graphs
But not all graphs are trees
• A tree is a connected graph without cycles• Traversals
Preorder, inorder, postorder traversals are examples of depth-first traversal
Level-order traversal of a tree is an example of breadth-first traversal
• Visit a node For a tree: process the node's data For a graph: mark the node as visited
![Page 17: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/17.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Trees
Fig. 30-9 The visitation order of two traversals; (a) depth first
![Page 18: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/18.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Trees
Fig. 30-9 The visitation order of two traversals; (b) breadth first.
![Page 19: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/19.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Breadth-First Traversal• A breadth-first traversal
visits a vertex and then each of the vertex's neighbors before advancing
• View algorithm for breadth-first traversal of nonempty graph beginning at a given vertex
![Page 20: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/20.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Breadth-First Traversal
Fig. 30-10 (ctd.) A trace of a breadth-first
traversal for a directed graph,
beginning at vertex A.
![Page 21: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/21.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Depth-First Traversal
• Visits a vertex, then A neighbor of the vertex, A neighbor of the neighbor, Etc.
• Advance as possible from the original vertex
• Then back up by one vertex Considers the next neighbor
• View algorithm for depth-first traversal
![Page 22: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/22.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Depth-First TraversalFig. 30-11 A
trace of a depth-first traversal beginning at
vertex A of the directed graph
![Page 23: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/23.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Topological Order
• Given a directed graph without cycles
• In a topological order Vertex a precedes vertex b whenever A directed edge exists from a to b
![Page 24: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/24.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Topological Order
Fig. 30-12 Three topological orders for the graph of Fig. 30-8.
Fig. 30-8
![Page 25: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/25.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Topological Order
Fig. 30-13 An impossible prerequisite structure for three courses as a directed graph with a cycle.
Click to view algorithm for a topological sort
Click to view algorithm for a topological sort
![Page 26: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/26.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Topological Order
Fig. 30-14 Finding a topological order for the graph in
Fig. 30-8.
![Page 27: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/27.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Unweighted Graph
Fig. 30-15 (a) an unweighted graph and (b) the possible paths from vertex A to vertex H.
![Page 28: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/28.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Unweighted Graph
Fig. 30-16 (a) The graph in 30-15a after the shortest-path algorithm has traversed from vertex A to vertex H;
(b) the data in the vertex
Click to view algorithm for finding shortest pathClick to view algorithm for finding shortest path
![Page 29: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/29.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Unweighted Graph
Fig. 30-17 Finding the shortest path from vertex A to vertex H in the unweighted graph
![Page 30: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/30.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Weighted Graph
Fig. 30-18 (a) A weighted graph and (b) the possible paths from vertex A to vertex H.
![Page 31: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/31.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Weighted Graph
• Shortest path between two given vertices Smallest edge-weight sum
• Algorithm based on breadth-first traversal
• Several paths in a weighted graph might have same minimum edge-weight sum Algorithm given by text finds only one of these
paths
![Page 32: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/32.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Weighted Graph
Fig. 30-19 Finding the cheapest path from vertex A to vertex H in the weighted graph
![Page 33: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/33.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Shortest Path in an Weighted Graph
Fig. 30-20 The graph in Fig. 30-18a after finding the cheapest path from vertex A to vertex H.
Click to view algorithm for
finding cheapest path in a weighted
graph
Click to view algorithm for
finding cheapest path in a weighted
graph
![Page 34: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/34.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Java Interfaces for the ADT Graph
• Methods in the BasicGraphInterface addVertex addEdge hasEdge isEmpty getNumberOfVertices getNumberOfEdges clear
• View interface for basic graph operations
![Page 35: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/35.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Java Interfaces for the ADT Graph
Fig. 30-21 A portion of the flight map in Fig. 30-6.
![Page 36: Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall.](https://reader030.fdocuments.net/reader030/viewer/2022032703/56649d365503460f94a0e4b9/html5/thumbnails/36.jpg)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Java Interfaces for the ADT Graph
• Operations of the ADT
Graph enable creation of a graph and
Answer questions based on relationships among vertices
• View interface of operations on an existing graph