Download presentation

Presentation is loading. Please wait.

Published byBarnard Robinson Modified about 1 year ago

1
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X

2
Chapter Contents Some Examples and Terminology – Road Maps – Airline Routes – Mazes – Course Prerequisites – Trees Traversals – Breadth-First Traversal – Dept-First Traversal

3
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

4
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

5
Road Maps Fig A portion of a road map. Nodes Edges

6
Road Maps Fig A directed graph representing a portion of a city's street map.

7
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

8
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

9
Weights Fig A weighted graph.

10
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

11
Connected Graphs Fig Undirected graphs

12
Adjacent Vertices Two vertices are adjacent in an undirected graph if they are joined by an edge Sometimes adjacent vertices are called neighbors Fig Vertex A is adjacent to B, but B is not adjacent to A.

13
Airline Routes Note the graph with two subgraphs – Each subgraph connected – Entire graph disconnected Fig Airline routes

14
Mazes Fig (a) A maze; (b) its representation as a graph

15
Course Prerequisites Fig The prerequisite structure for a selection of courses as a directed graph without cycles.

16
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

17
Trees Fig The visitation order of two traversals; (a) depth first

18
Trees Fig The visitation order of two traversals; (b) breadth first.

19
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 View algorithm

20
Breadth-First Traversal Fig (ctd.) A trace of a breadth-first traversal for a directed graph, beginning at vertex A.

21
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 View algorithm

22
Depth-First Traversal Fig A trace of a depth-first traversal beginning at vertex A of the directed graph

23
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

24
Topological Order Fig Three topological orders for the graph of Fig Fig. 30-8

25
Topological Order Fig An impossible prerequisite structure for three courses as a directed graph with a cycle. Click to view algorithm for a topological sort

26
Topological Order Fig Finding a topological order for the graph in Fig

27
Shortest Path in an Unweighted Graph Fig (a) an unweighted graph and (b) the possible paths from vertex A to vertex H.

28
Shortest Path in an Unweighted Graph Fig (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 path

29
Shortest Path in an Unweighted Graph Fig Finding the shortest path from vertex A to vertex H in the unweighted graph

30
Shortest Path in an Weighted Graph Fig (a) A weighted graph and (b) the possible paths from vertex A to vertex H.

31
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

32
Shortest Path in an Weighted Graph Fig Finding the cheapest path from vertex A to vertex H in the weighted graph

33
Shortest Path in an Weighted Graph Fig The graph in Fig a after finding the cheapest path from vertex A to vertex H. Click to view algorithm for finding cheapest path in a weighted graph

34
Java Interfaces for the ADT Graph Methods in the BasicGraphInterface – addVertex – addEdge – hasEdge – isEmpty – getNumberOfVertices – getNumberOfEdges – clear View interface for basic graph operations View interface

35
Java Interfaces for the ADT Graph Fig A portion of the flight map in Fig

36
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 View interface

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google