graphs
Download
Skip this Video
Download Presentation
Graphs

Loading in 2 Seconds...

play fullscreen
1 / 34

Graphs - PowerPoint PPT Presentation


  • 110 Views
  • Uploaded on

Graphs. Chapter 29. Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First Traversal Dept-First Traversal. Topological Order Paths Finding a Path Shortest Path in an Unweighted Graph Shortest Pat in a Weighted Graph

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Graphs' - naida-alvarado


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
graphs

Graphs

Chapter 29

chapter contents
Some Examples and Terminology

Road Maps

Airline Routes

Mazes

Course Prerequisites

Trees

Traversals

Breadth-First Traversal

Dept-First Traversal

Topological Order

Paths

Finding a Path

Shortest Path in an Unweighted Graph

Shortest Pat in a Weighted Graph

Java Interfaces for the ADT Graph

Chapter Contents
some examples and terminology
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
road maps
Road Maps

Nodes

Edges

Fig. 29-1 A portion of a road map.

road maps1
Road Maps

Fig. 29-2 A directed graph representing a portion of a city's street map.

paths
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
weights
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
weights1
Weights

Fig. 29-3 A weighted graph.

connected graphs
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
connected graphs1
Connected Graphs

Fig. 29-4 Undirected graphs

adjacent vertices
Adjacent Vertices
  • Two vertices are adjacent in an undirected graph if they are joined by an edge
  • Sometimes adjacent vertices are called neighbors

Fig. 29-5 Vertex A is adjacent to B, but B is not adjacent to A.

airline routes
Airline Routes
  • Note the graph with two subgraphs
    • Each subgraph connected
    • Entire graph disconnected

Fig. 29-6 Airline routes

mazes
Mazes

Fig. 29-7 (a) A maze; (b) its representation as a graph

course prerequisites
Course Prerequisites

Fig. 29-8 The prerequisite structure for a selection of courses as a directed graph without cycles.

trees
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
trees1
Trees

Fig. 29-9 The visitation order of two traversals; (a) depth first; (b) breadth first.

breadth first traversal
Breadth-First Traversal
  • Algorithm for breadth-first traversal of nonempty graph beginning at a given vertex

Algorithm getBreadthFirstTraversal(originVertex)vertexQueue = a new queue to hold neighborstraversalOrder = a new queue for the resulting traversal orderMark originVertex as visitedtraversalOrder.enqueue(originVertex)vertexQueue.enqueue(originVertex)while (!vertexQueue.isEmpty()){ frontVertex = vertexQueue.dequeue()while (frontVertex has an unvisited neighbor) { nextNeighbor = next unvisited neighbor of frontVertexMark nextNeighbor as visitedtraversalOrder.enqueue(nextNeighbor) vertexQueue.enqueue(nextNeighbor) }}return traversalOrder

A breadth-first traversal visits a vertex and then each of the vertex's neighbors before advancing

breadth first traversal1
Breadth-First Traversal

Fig. 29-10 (ctd.) A trace of a breadth-first traversal for a directed graph, beginning at vertex A.

depth first traversal
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
depth first traversal1
Depth-First Traversal

Fig. 29-11 A trace of a depth-first traversal beginning at vertex A of the directed graph in Fig. 29-10a.

topological order
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
topological order1
Topological Order

Fig. 29-12 Three topological orders for the graph of Fig. 29-8.

topological order2
Topological Order

Fig. 29-13 An impossible prerequisite structure for three courses as a directed graph with a cycle.

topological order3
Topological Order
  • Algorithm for a topological sort

Algorithm getTopologicalSort()vertexStack = a new stack to hold vertices as they are visitedn = number of vertices in the graphfor (counter = 1 to n){ nextVertex = an unvisited vertex whose neighbors, if any, are all visited Mark nextVertex as visitedstack.push(nextVertex)}return stack

topological order4
Topological Order

Fig. 29-14 Finding a topological order for the graph in Fig. 29-8.

shortest path in an unweighted graph
Shortest Path in an Unweighted Graph

Fig. 29-15 (a) an unweighted graph and (b) the possible paths from vertex A to vertex H.

shortest path in an unweighted graph1
Shortest Path in an Unweighted Graph

Fig. 29-16 The graph in 29-15a after the shortest-path algorithm has traversed from vertex A to vertex H

shortest path in an unweighted graph2
Shortest Path in an Unweighted Graph

Fig. 29-17 Finding the shortest path from vertex A to vertex H in the unweighted graph in Fig. 29-15a.

shortest path in an weighted graph
Shortest Path in an Weighted Graph

Fig. 29-18 (a) A weighted graph and (b) the possible paths from vertex A to vertex H.

shortest path in an weighted graph1
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
shortest path in an weighted graph2
Shortest Path in an Weighted Graph

Fig. 29-19 Finding the cheapest path from vertex A to vertex H in the weighted graph in Fig 29-18a.

shortest path in an weighted graph3
Shortest Path in an Weighted Graph

Fig. 29-20 The graph in Fig. 29-18a after finding the cheapest path from vertex A to vertex H.

java interfaces for the adt graph
Java Interfaces for the ADT Graph
  • Methods in the BasicGraphInterface
    • addVertex
    • addEdge
    • hasEdge
    • isEmpty
    • getNumberOfVertices
    • getNumberOfEdges
    • clear
java interfaces for the adt graph1
Java Interfaces for the ADT Graph

Operations of the ADT graph enable creation of a graph and answer questions based on relationships among vertices

Fig. 29-21 A portion of the flight map in Fig. 29-6.

ad