Presentation on theme: "Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X."— Presentation transcript:
1GraphsChapter 30Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X
2Chapter Contents Some Examples and Terminology Traversals Road Maps Airline RoutesMazesCourse PrerequisitesTreesTraversalsBreadth-First TraversalDept-First Traversal
3Chapter Contents Topological Order Paths Finding a PathShortest Path in an Unweighted GraphShortest Path in a Weighted GraphJava Interfaces for the ADT Graph
4Some Examples and Terminology Vertices or nodes are connected by edgesA graph is a collection of distinct vertices and distinct edgesEdges can be directed or undirectedWhen it has directed edges it is called a digraphA subgraph is a portion of a graph that itself is a graph
6Road MapsFig A directed graph representing a portion of a city's street map.
7Paths A sequence of edges that connect two vertices in a graph In a directed graph the direction of the edges must be consideredCalled a directed pathA cycle is a path that begins and ends at same vertexSimple path does not pass through any vertex more than onceA graph with no cycles is acyclic
8Weights A weighted graph has values on its edges Weights or costsA path in a weighted graph also has weight or costThe sum of the edge weightsExamples of weightsMiles between nodes on a mapDriving time between nodesTaxi cost between node locations
10Connected Graphs A connected graph A complete graph Has a path between every pair of distinct verticesA complete graphHas an edge between every pair of distinct verticesA disconnected graphNot connected
12Adjacent VerticesTwo vertices are adjacent in an undirected graph if they are joined by an edgeSometimes adjacent vertices are called neighborsFig Vertex A is adjacent to B, but B is not adjacent to A.
13Airline Routes Note the graph with two subgraphs Each subgraph connectedEntire graph disconnectedFig Airline routes
14MazesFig (a) A maze; (b) its representation as a graph
15Course PrerequisitesFig The prerequisite structure for a selection of courses as a directed graph without cycles.
16Trees All trees are graphs A tree is a connected graph without cycles But not all graphs are treesA tree is a connected graph without cyclesTraversalsPreorder, inorder, postorder traversals are examples of depth-first traversalLevel-order traversal of a tree is an example of breadth-first traversalVisit a nodeFor a tree: process the node's dataFor a graph: mark the node as visited
17TreesFig The visitation order of two traversals; (a) depth first
18TreesFig The visitation order of two traversals; (b) breadth first.
19Breadth-First Traversal A breadth-first traversalvisits a vertex andthen each of the vertex's neighborsbefore advancingView algorithm for breadth-first traversal of nonempty graph beginning at a given vertex
20Breadth-First Traversal Fig (ctd.) A trace of a breadth-first traversal for a directed graph, beginning at vertex A.
21Depth-First Traversal Visits a vertex, thenA neighbor of the vertex,A neighbor of the neighbor,Etc.Advance as possible from the original vertexThen back up by one vertexConsiders the next neighborView algorithm for depth-first traversal
22Depth-First Traversal Fig A trace of a depth-first traversal beginning at vertex A of the directed graph
23Topological Order Given a directed graph without cycles In a topological orderVertex a precedes vertex b wheneverA directed edge exists from a to b
24Topological OrderFig. 30-8Fig Three topological orders for the graph of Fig
25Topological OrderFig An impossible prerequisite structure for three courses as a directed graph with a cycle.Click to view algorithm for a topological sort
26Topological OrderFig Finding a topological order for the graph in Fig
27Shortest Path in an Unweighted Graph Fig (a) an unweighted graph and (b) the possible paths from vertex A to vertex H.
28Shortest Path in an Unweighted Graph Click to view algorithm for finding shortest pathFig (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
29Shortest Path in an Unweighted Graph Fig Finding the shortest path from vertex A to vertex H in the unweighted graph
30Shortest Path in an Weighted Graph Fig (a) A weighted graph and (b) the possible paths from vertex A to vertex H.
31Shortest Path in an Weighted Graph Shortest path between two given verticesSmallest edge-weight sumAlgorithm based on breadth-first traversalSeveral paths in a weighted graph might have same minimum edge-weight sumAlgorithm given by text finds only one of these paths
32Shortest Path in an Weighted Graph Fig Finding the cheapest path from vertex A to vertex H in the weighted graph
33Shortest Path in an Weighted Graph Click to view algorithm for finding cheapest path in a weighted graphFig The graph in Fig a after finding the cheapest path from vertex A to vertex H.
34Java Interfaces for the ADT Graph Methods in the BasicGraphInterfaceaddVertexaddEdgehasEdgeisEmptygetNumberOfVerticesgetNumberOfEdgesclearView interface for basic graph operations
35Java Interfaces for the ADT Graph Fig A portion of the flight map in Fig
36Java Interfaces for the ADT Graph Operations of the ADTGraph enable creation of a graph andAnswer questions based on relationships among verticesView interface of operations on an existing graph