Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
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 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
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.
Graph & BFS.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Representing and Using Graphs
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
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++
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 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.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Introduction to Algorithms
Graphs Chapter 20.
CSE 373 Topological Sort Graph Traversals
Lecture 11 Graph Algorithms
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Short paths and spanning trees
CHP-7 GRAPH.
Graph Algorithm.
Graph & BFS.
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Graphs.
Chapter 11 Graphs.
Shortest Path Algorithms
Shortest Paths and Minimum Spanning Trees
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
ITEC 2620M Introduction to Data Structures
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Text Book: Introduction to algorithms By C L R S
Graph Implementation.
Graphs.
CE 221 Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
For Friday Read chapter 9, sections 2-3 No homework
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview 15.1 15.2 15.3 Discussion of graph terminology. Representation 15.3 Traversal

Overview 15.5 15.6 Algorithms for finding shortest paths Minimum spanning tree Graphs generalize trees. Graphs may have more than one path between two nodes.

Terminology Graph Consists of vertices (nodes) and edges. Vertices are circles. Edges are lines or arrows.

Terminology Directed graph Undirected graph Edge is an arrow going from one vertex to another. In some situations, it is meaningful to have a self- loop. Undirected graph Edge is simply an line connecting two vertices. Undirected graphs usually do not have self-loops. For every undirected graph there is a corresponding directed graph where each undirected edge is replaced by two directed edges, one in each direction.

Terminology

Terminology Neighbors Path Length Vertices that can be reached by moving along one edge. Path A sequence of vertices, each a neighbor of the previous one. Length Length of the path is the number of edges along the path.

Terminology Distance Cycle Weighted graphs Distance between two vertices is the length of the shortest path between them. Cycle A path (of length one or more) from a vertex back to itself. Weighted graphs Data associated with the edges.

Terminology

Terminology

Terminology

Terminology

Terminology A graph with no cycles is called acyclic. dag Directed acyclic graph

Terminology A graph in which every pair of vertices is connected by a path is said to be connected.

Terminology Weighted graph. We can associate data with an edge.

Terminology When analyzing graph algorithms, we often give results in terms of v (the number of vertices) and e (the number of edges). In a directed graph, e ≤ v2. In an undirected graph:

Terminology In either case e O(v2) Dense Sparse A graph with close to the maximum number of edges. Sparse A graph with far fewer edges.

Representation Neighbor list or adjacency list The amount of memory used is in Θ(v + e). The time to check for a particular edge is in O(e).

Representation If a graph is dense, it is better to use a neighbor matrix or adjacency matrix. This is essentially a two-dimensional array of booleans. <i, j> is true when there is an edge from vertex i to vertex j. Memory used is Θ(n2). The time to check for a particular edge is constant. Easily adapted to handle weighted graphs. 0 if there is no edge.

Representation

Representation

Representation

Representation

Representation

Representation

Representation

Representation

Representation

Representation

Representation

Graph Traversal Source vertex Depth-first traversal Breadth-first We must choose an arbitrary vertex to start from. Depth-first traversal Follows edges until it reaches a dead end, then backtracks to the last branching point. Breadth-first Visits one vertex, then its neighbors, then vertices two edges away, and so on.

Graph Traversal Vertices that cannot be reached from the source are never visited. It is necessary to keep track of which vertices have already been visited. This is done with an array of booleans.

Graph Traversal Depth-first

Graph Traversal Breadth-first

Graph Traversal

Graph Traversal

Graph Traversal

Shortest Paths A common graph problem is finding the shortest path between two vertices. We want to minimize the sum of the weghts of the edges on the path, not the number of vertices. How much does it cost to get directly from one vertex to another. Weight of the edge is the cost. If they are not connected, the cost is infinite. Double.POSTITIVE_INFINITY Double.POSTITIVE_INFINITY + 1 == Double.POSTIVITE_INFINITY

Shortest Paths

Shortest Paths Two algorithms Dijkstra's single source algorithm determines the distances from one vertex to all others. Floyd-Warshall all pairs algorithm, which might be used to create a table of distances in a road atlas, determines the distance from each vertex to each other vertex.

Shortest Paths Dijkstra's Single-Source Algorithm Maintains an array containing the distance to each vertex. Initially, the distance to the source vertex is 0 and all other distances are infinite.

Shortest Paths The vertices are visited in order of increasing distance from the source. By the time we visit a vertex, we have visited all of the vertices along the shortest path to that vertex. Similar to a breadth-first traversal, but the weights are taken into account. result[i] + getCost(i, j) If this sum is smaller, we have found a shorter path to j, so we update result[j] Total running time Θ(v2)

Shortest Paths

Shortest Paths

Shortest Paths Floyd-Warshall All-Pairs Algorithm To find the shorest path between each pair of vertices, we could just run Dijkstra's algorithm once from each vertex, using time in Θ(v3) Floyd-Warshall all-pairs takes time in this order, but it is somewhat simpler. Uses dynamic programming. result[i] [j] is the shortest known distance from vertex i to vertex j.

Shortest Paths Questions to ask: What is the shortest distance between each pair of vertices using vertex 0 as an intermediate point? What is the shortest distance between each pair of vertices using vertices 0 and 1 as intermediate points? What is the shortest distance between each pair of vertices using vertices 0 through 2 as intermediate points?

Shortest Paths When possible intermediate points have been considered the distances are correct. Two possibilities for each pair of vertices i and j: The shortest path using vertices 0 through 5 as intermediate points does not involve vertex 5 (It was already correct). The shortest path using vertices 0 through 5 as intermediate points does involve vertex 5. It must be result[i][5] + result[5][j]. Neither of these two subpaths can have vertex 5 as an intermediate point, because it is an endpoint. Running time in Θ(v3)

Shortest Paths

Shortest Paths

Minimum Spanning Trees Connect a set of vertices while minimizing expenses. Spanning tree Given a connected, undirected graph G, a spanning tree is another graph which has all of G's vertices and a subset of G's edges Spanning tree has only v – 1 edges. Minimum number of edges required to connect all of the vertices. Never contains a cycle. May be more than one spanning tree for a given graph.

Minimum Spanning Trees In a weighted graph, a minimum spanning tree is a spanning tree in which the sum of the edge weight is as small as possible. There may be more than one minimum spanning tree for a given graph. Kruskal's minimum spanning tree algorithm. Begin with an edgeless graph, then add edges until we have a minimum spanning tree. Add the cheapest edge that we haven't tried before. If there was not already a path between the vertices at the ends of the edge, we add the edge.

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees This is a Greedy algorithm Always does whatever seems best in the short term.

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees Total running time:

Summary A graph is a collection of vertices connected by edges. Directed or undirected, weighted or unweighted, and connected or unconnected. A graph with no cycles is called acyclic. Graphs are normally represented using neighbor lists or neighbor matrices. Neighbor lists use less space for sparse graphs. Neighbor matrices take less time in either case.

Summary A graph may be traversed depth-first. A topological sort of a directed acyclic graph is an ordering of the vertices such that no edges point from a later vertex to an earlier vertex. Finding shortest paths in a weighted graph eliminates some edges so as to stay connected while minimizing the total edge weight. Kruskal's minimum spanning tree algorithm is a greedy algorithm.