Computer Science 112 Fundamentals of Programming II Graph Algorithms.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Fundamentals of Python: From First Programs Through Data Structures
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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.
CS202 - Fundamental Structures of Computer Science II
Graphs CS3240, L. grewe.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph & BFS.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Important Problem Types and Fundamental Data Structures
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
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
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Computer Science 112 Fundamentals of Programming II Introduction to 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,
Some material taken from website of Dale Winter.  Leonhard Euler – 1736  Find a starting point so that you can walk around the city, crossing each bridge.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
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.
COSC 2007 Data Structures II
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.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CS 367 Introduction to Data Structures Lecture 13.
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.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Section 9.6 Graph Applications. 9.6 Graph Applications Our graph specification does not include traversal operations. We treat traversal as a graph application/algorithm.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CSE 373 Topological Sort Graph Traversals
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
Graphs Representation, BFS, DFS
"Learning how to learn is life's most important skill. " - Tony Buzan
Graph Representation (23.1/22.1)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Presentation transcript:

Computer Science 112 Fundamentals of Programming II Graph Algorithms

Basic Graph Algorithms Traversal Search for an item starting from a given vertex Find all of the vertices to which a given vertex is connected by paths Find the shortest path between two vertices Find the shortest paths between one vertex and all the other vertices

A Generic Graph Traversal Start at a given vertex Visit all the vertices reachable from the starting vertex Might not visit all the vertices in the graph

traverseFromVertex(graph, startVertex) Instantiate a collection (a list will do for now) Mark all vertices in the graph as unvisited Add the startVertex to the collection While the collection is not empty Pop the vertex from the collection If the vertex has not been visited Mark the vertex as visited Process the vertex Add all adjacent unvisited vertices to the collection A Generic Traversal Algorithm

Depth-First and Breadth-First Traversals Use a stack to force the traversal to move deeply into the graph before backtracking to another path Use a queue to force the traversal to visit all adjacent vertices before moving to the next level

traverseFromVertex(graph, startVertex, collection) Mark all vertices in the graph as unvisited Add the startVertex to the collection While the collection is not empty Pop the vertex from the collection If the vertex has not been visited Mark the vertex as visited Process the vertex Add all adjacent unvisited vertices to the collection Add a Collection Parameter

traverseFromVertex(graph, startVertex, collection) Mark all vertices in the graph as unvisited Add the startVertex to the collection While the collection is not empty Pop the vertex from the collection If the vertex has not been visited Mark the vertex as visited Process the vertex Add all adjacent unvisited vertices to the collection depthFirstTraverse(graph, startVertex) traverseFromVertex(graph, startVertex, ArrayStack()) Depth-First: Use a Stack

traverseFromVertex(graph, startVertex, collection) Mark all vertices in the graph as unvisited Add the startVertex to the collection While the collection is not empty Pop the vertex from the collection If the vertex has not been visited Mark the vertex as visited Process the vertex Add all adjacent unvisited vertices to the collection depthFirstTraverse(graph, startVertex) traverseFromVertex(graph, startVertex, ArrayStack()) breadthFirstTraverse(graph, startVertex) traverseFromVertex(graph, startVertex, LinkedQueue()) Breadth-First: Use a Queue

depthFirstSearch(graph, startVertex) Mark all vertices in the graph as unvisited dfs(graph, startVertex) dfs(graph, v) Mark v as visited Process v For each vertex w adjacent to v If w has not been visited dfs(graph, w) Recursive Depth-First Traversal

Topological Ordering The vertices in a directed acyclic graph have an ordering Example: the courses for a CS major have prerequisites In what order can I take a given set of courses?

Topological Sort Traverse the graph and assign a numeric rank, in ascending order, to the vertices There might be more than one such ordering for a given DAG

Example

topologicalSort(graph) Instantiate a stack Mark all vertices in the graph as unvisited For each vertex v in the graph If v is unvisited dfs(graph, v, stack) Return stack dfs(graph, v, stack) Mark v as visited For each vertex w adjacent to v If w has not been visited dfs(graph, w, stack) stack.push(v) Topological Sort Algorithm

All-Pairs Shortest-Paths Problem Given a weighted graph, find the shortest paths between each pair of vertices for which there is a path Useful for planning or scheduling trips between cities, designing networks, etc.

Graph shows vertices and edges; weights are yet to be filled in (distances between adjacent cities) Distance matrix shows distances (weights) between adjacent vertices; ∞ means distance of a path not yet known

All-pairs algorithm fills in the shortest distances between all the pairs

Floyd’s Algorithm Published by Robert Floyd in 1962 Inputs: N by N matrix A for a graph of N vertices Outputs: Same matrix, but with the length of each path replaced by the length of the shortest path, or ∞ if there is no path

Floyd’s Algorithm for i = 0 to n – 1 for r = 0 to n – 1 for c = 0 to n – 1 A rc = min(A rc, A ri + A ic ) A rc = distance from vertex r to vertex c A ri = distance from vertex r to vertex i A ic = distance from vertex i to vertex c Runtime complexity?

Single-Source Shortest-Paths Problem Given a weighted directed acyclic graph, find the shortest paths from a single source vertex to the other vertices for which there are paths Useful for planning or scheduling trips between cities, designing networks, etc.

Dykstra’s Algorithm Output is a two-dimensional list with N rows and 3 columns –First column: a vertex –Second column: the distance from the source vertex –Third column: the immediate predecessor on this path Also uses a list of Booleans to track the status of the search for each vertex

The Initialization Step for each vertex in the graph Store vertex in the current row of the results list If vertex = source vertex Set the row's distance cell to 0 Set the row's path cell to undefined Set included[row] to True Else if there is an edge from source vertex to vertex Set the row's distance cell to the edge's weight Set the row's path cell to source vertex Set included[row] to False Else Set the row's distance cell to infinity Set the row's path cell to undefined Set included[row] to False Runtime complexity?

The Initial State of the Data Structures

The Final State of the Data Structures

The Computation Step Do Find the vertex F that is not yet included and has the minimal distance Mark F as included For each other vertex T not included If there is an edge from F to T Set new distance to F's distance + edge's weight If new distance < T's distance in the results array Set T's distance to new distance Set T's path in the results array to F While all vertices are not included Runtime complexity?

For Wednesday Linked Directed Graph Implementation