Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Slides:



Advertisements
Similar presentations
Part A - Terminology and Traversals
Advertisements

CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 27 Graphs and Applications.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
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.
James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
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.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 9: Graphs Basic Concepts
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 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,
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
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.
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. 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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Chapter 20.
Graphs.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Introduction (with applications) ADT & terminology
CS120 Graphs.
Graphs Chapter 13.
Slides by Steve Armstrong LeTourneau University Longview, TX
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 9: Graphs Basic Concepts
Chapter 11 Graphs.
Stack Implementations
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 9: Graphs Basic Concepts
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents Some Examples and Terminology  Road Maps  Airline Routes  Mazes  Course Prerequisites  Trees Traversals  Breadth-First Traversal  Dept-First Traversal

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Road Maps Fig A portion of a road map. Nodes Edges

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Road Maps Fig A directed graph representing a portion of a city's street map.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Weights Fig A weighted graph.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Connected Graphs Fig Undirected graphs

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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 Vertex A is adjacent to B, but B is not adjacent to A.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Airline Routes Note the graph with two subgraphs  Each subgraph connected  Entire graph disconnected Fig Airline routes

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Mazes Fig (a) A maze; (b) its representation as a graph

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Course Prerequisites Fig The prerequisite structure for a selection of courses as a directed graph without cycles.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Trees Fig The visitation order of two traversals; (a) depth first

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Trees Fig The visitation order of two traversals; (b) breadth first.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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 vertexView algorithm

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Breadth-First Traversal Fig (ctd.) A trace of a breadth-first traversal for a directed graph, beginning at vertex A.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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 traversalView algorithm

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Depth-First Traversal Fig A trace of a depth- first traversal beginning at vertex A of the directed graph

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Topological Order Fig Three topological orders for the graph of Fig Fig. 30-8

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Topological Order Fig Finding a topological order for the graph in Fig

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Shortest Path in an Unweighted Graph Fig (a) an unweighted graph and (b) the possible paths from vertex A to vertex H.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Shortest Path in an Unweighted Graph Fig Finding the shortest path from vertex A to vertex H in the unweighted graph

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Shortest Path in an Weighted Graph Fig (a) A weighted graph and (b) the possible paths from vertex A to vertex H.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Shortest Path in an Weighted Graph Fig Finding the cheapest path from vertex A to vertex H in the weighted graph

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Java Interfaces for the ADT Graph Methods in the BasicGraphInterface  addVertex  addEdge  hasEdge  isEmpty  getNumberOfVertices  getNumberOfEdges  clear View interface for basic graph operationsView interface

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Java Interfaces for the ADT Graph Fig A portion of the flight map in Fig

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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 graphView interface