CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.

Slides:



Advertisements
Similar presentations
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2011W2.
Advertisements

Lecture 15. Graph Algorithms
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Dan Grossman Spring 2010.
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.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CSE 326: Data Structures Lecture #21 Graphs II.5 Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
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.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
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.
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.
CS 146: Data Structures and Algorithms July 21 Class Meeting
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.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
CSE332: Data Structures & Algorithms Lecture 13: Introduction to Graphs Dan Grossman Fall 2013.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
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.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Nicki Dell Spring 2014.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CSE332: Data Structures & Algorithms Lecture 14: Introduction to Graphs Aaron Bauer Winter 2014.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
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.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CSE332: Data Structures & Algorithms Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS
Directed Graph Algorithms
Directed Graph Algorithms
Based on slides of Dan Suciu
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
Richard Anderson Spring 2016
Directed Graphs (Part II)
Presentation transcript:

CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001

Outline Some definitions Topological Sort Graph Data Structures Graph Properties Shortest Path Problem

Directed vs. Undirected Graphs Han Leia Luke Han Leia Luke aka: di-graphs In directed graphs, edges have a specific direction: In undirected graphs, they don’t (edges are two-way): Vertices u and v are adjacent if (u, v)  E

Weighted Graphs Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton There may be more information in the graph as well. Each edge has an associated weight or cost.

Paths A path is a list of vertices {v 1, v 2, …, v n } such that (v i, v i+1 )  E for all 0  i < n. Seattle San Francisco Dallas Chicago Salt Lake City p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

Path Length and Cost Path length: the number of edges in the path Path cost: the sum of the costs of each edge Seattle San Francisco Dallas Chicago Salt Lake City length(p) = 5cost(p) = 11.5

Simple Paths and Cycles A simple path repeats no vertices (except that the first can be the last): –p = {Seattle, Salt Lake City, San Francisco, Dallas} –p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A cycle is a path that starts and ends at the same node: –p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A simple cycle is a cycle that repeats no vertices except that the first vertex is also the last (in undirected graphs, no edge can be repeated)

Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Di-graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices

Graph Density A sparse graph has O(|V|) edges A dense graph has  (|V| 2 ) edges Anything in between is either sparsish or densy depending on the context. We use V and E as the parameters in our analysis.

Trees as Graphs Every tree is a graph with some restrictions: –the tree is directed –there are no cycles (directed or undirected) –there is a directed path from the root to every node A B DE C F HG JI

Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no cycles. main() add() access() mult() read() Trees  DAGs  Graphs

Topological Sort Given a graph, G = (V, E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it. check in airport call taxi taxi to airport reserve flight pack bags take flight locate gate

Topo-Sort Take One Label each vertex’s in-degree (# of inbound edges) While there are vertices remaining Pick a vertex with in-degree of zero and output it Reduce the in-degree of all vertices adjacent to it Remove it from the list of vertices runtime:

Topo-Sort Take Two Label each vertex’s in-degree Initialize a queue to contain all in-degree zero vertices While there are vertices remaining in the queue Pick a vertex v with in-degree of zero and output it Reduce the in-degree of all vertices adjacent to v Put any of these with new in-degree zero on the queue Remove v from the queue runtime:

Graph Representations List of vertices + list of edges 2-D matrix of vertices (marking edges in the cells) “adjacency matrix” List of vertices each with a list of adjacent vertices “adjacency list” Han Leia Luke

Adjacency Matrix A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to v Han Leia Luke HanLukeLeia Han Luke Leia runtime:space requirements:

Adjacency List A |V| -ary list (array) in which each entry stores a list (linked list) of all adjacent vertices Han Leia Luke Han Luke Leia runtime:space requirements:

Single Source, Shortest Path Given a graph G = (V, E) and a vertex s  V, find the shortest path from s to every vertex in V Many variations: –weighted vs. unweighted –cyclic vs. acyclic –positive weights only vs. negative weights allowed –multiple weight types to optimize

The Trouble with Negative Weighted Cycles AB CD E What’s the shortest path from A to E? (or to B, C, or D, for that matter)

Unweighted Shortest Path Problem Assume source vertex is C … A C B D FH G E Distance to: A B C D E F G H

Dijkstra Legendary figure in computer science; now a professor at University of Texas. Supports teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until recently) read his ; so, his staff had to print out messages and put them in his box.

Dijkstra’s Algorithm for Single Source Shortest Path Classic algorithm for solving shortest path in weighted graphs without negative weights A greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition: –shortest path from source vertex to itself is 0 –cost of going to adjacent nodes is at most edge weights –cheapest of these must be shortest path to that node –update paths for new node and continue picking cheapest path

Intuition in Action A C B D FH G E

Dijkstra’s Pseudocode (actually, our pseudocode for Dijkstra’s algorithm) Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node with the lowest cost: n Mark n as known For each node a which is adjacent to n a’s cost = min(a’s old cost, n’s cost + cost of (n, a))

Dijkstra’s Algorithm in Action A C B D FH G E

THE KNOWN CLOUD G Next shortest path from inside the known cloud P Better path to the same node The Cloud Proof But, if the path to G is the next shortest path, the path to P must be at least as long. So, how can the path through P to G be shorter? Source

Inside the Cloud (Proof) Everything inside the cloud has the correct shortest path Proof is by induction on the # of nodes in the cloud: –initial cloud is just the source with shortest path 0 –inductive step: once we prove the shortest path to G is correct, we add it to the cloud Negative weights blow this proof away!

Revenge of the Dijkstra Pseudocode Initialize the cost of each node to  s.cost = 0; heap.insert(s); While (! heap.empty) n = heap.deleteMin() For (each node a which is adjacent to n) if (n.cost + edge[n,a].cost < a.cost) then a.cost = n.cost + edge[n,a].cost a.path = n; if (a is in the heap) then heap.decreaseKey(a) else heap.insert(a)

Data Structures for Dijkstra’s Algorithm Select the unknown node with the lowest cost findMin/deleteMin a’s cost = min(a’s old cost, …) decreaseKey find by name |V| times: |E| times: runtime: O(log |V|) O(1)