Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

Similar documents
Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

CSE 332 Data Abstractions: Graphs and Graph Traversals

Shortest Paths. CSE 373 Data Structures Lecture 21

Basic Graph Definitions

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Graphs: Topological Sort / Graph Traversals (Chapter 9)

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Graph Traversals. CS200 - Graphs 1

CSE 373: Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms

CS302 - Data Structures using C++

Graphs & Digraphs Tuesday, November 06, 2007

Lecture 9 Graph Traversal

CSE 373 Data Structures & Algorithms

Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

1. Graph and Representation

CSE 332: Data Structures & Parallelism Lecture 19: Introduction to Graphs. Ruth Anderson Autumn 2018

CSE 100: GRAPH ALGORITHMS

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun

Graphs: Definitions and Representations (Chapter 9)

CS 206 Introduction to Computer Science II

Inf 2B: Graphs, BFS, DFS

Graphs: Definitions and Representations (Chapter 9)

CS2 Algorithms and Data Structures Note 9

Outline. Graphs. Divide and Conquer.

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Graph G = (V, E) V = set of vertices (nodes) E = set of edges (arcs)(lines) (arrows) D F E A C

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Data Structures and Algorithms. Chapter 7. Graphs

Announcements Problem Set 4 is out!

GRAPHS Lecture 19 CS2110 Spring 2013

Breadth First Search. cse2011 section 13.3 of textbook

Introduction to Graphs. common/notes/ppt/

Graph. Vertex. edge. Directed Graph. Undirected Graph

CSE 100: GRAPH SEARCH

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Data Structures and Algorithms

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Chapter 9 Graph Algorithms

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part A

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

Lecture 26: Graphs: Traversal (Part 1)

Graphs. CS16: Introduction to Data Structures & Algorithms Spring 2018

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Introduction to Graphs. CS2110, Spring 2011 Cornell University

(Re)Introduction to Graphs and Some Algorithms

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Chapter 9. Priority Queue

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

CSC263 Week 8. Larry Zhang.

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Chapter 28 Graphs and Applications. Objectives

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

CSE 373 Final Exam 3/14/06 Sample Solution

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Graphs Introduction and Depth first algorithm

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Algorithm Design and Analysis

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Chapter 22 Elementary Graph Algorithms

Discrete Motion Planning

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Graphs Chapter 24. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

CS61BL. Lecture 5: Graphs Sorting

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

GRAPHS Lecture 17 CS2110 Spring 2014

csci 210: Data Structures Graph Traversals

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

W4231: Analysis of Algorithms

CS Algorithms and Complexity

VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur

Some Extra Information on Graph Search

The Shortest Path Problem

CS 561, Lecture 10. Jared Saia University of New Mexico

More Graph Algorithms

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Graphs Data Structures

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Directed Acyclic Graphs & Topological Sort

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Transcription:

Today s Outline Graphs Chapter 9 in Weiss CSE 6 Data Structures Ruth Anderson /6/00 Announcements Written Homework #6 due NOW Project Code due Mon March by pm Project Benchmarking & Written due Thurs March by pm Today s Topics: Sorting Graphs /6/00 Not quite an ADT operations not clear Graph ADT? A formalism for representing relationships between objects Graph G = (V,E) Set of : V = {v,v,,v n Set of edges: E = {e,e,,e m where each e i connects two (v i,v i ) /6/00 V = {,, E = {(, ), (, ), (, ) Graph Definitions In directed graphs, edges have a specific direction: In undirected graphs, they don t (edges are two-way): v is adjacent to u if (u,v) E /6/00 More Definitions: Simple Paths and Cycles Trees as Graphs A simple path repeats no (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 p = {Seattle, Salt Lake City, Seattle, San Francisco, Seattle A simple cycle is a cycle that repeats no except that the first vertex is also the last (in undirected graphs, no edge can be repeated) Every tree is a graph! Not all graphs are trees! A graph is a tree if There are no cycles (directed or undirected) There is a path from the root to every node D B A E G C F H /6/00 5 /6/00 6

Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no (directed) cycles. Aside: If program callgraph is a DAG, then all procedure calls can be inlined add() access() main() mult() read() /6/00 7 Graph Representations 0. List of + list of edges. -D matrix of (marking edges in the cells) adjacency matrix. List of each with a list of adjacent adjacency list Things we might want to do: iterate over iterate over edges iterate over adj. to a vertex check whether an edge exists Vertices and edges may be labeled /6/00 8 Representation : 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 Representation adjacency matrix: space requirements: runtime: /6/00 9 A[u][v] weight = 0, if, if /6/00 0 (u, (u, v) v) E E Representation adjacency list: Representation : Adjacency List A V -ary list (array) in which each entry stores a list (linked list) of all adjacent /6/00 space requirements: runtime: /6/00

Good match? List of edges and list of Adjacency matrix Adjacency list Moving Around Washington Iterate over Iterate over edges Check if edge exists Iterate over adjacent to a vertex What s the shortest way to get from Seattle to Pullman? Edge labels: /6/00 Moving Around Washington Reliability of Communication What s the fastest way to get from Seattle to Pullman? Edge labels: /6/00 5 If Wenatchee s phone exchange goes down, can Seattle still talk to Pullman? /6/00 6 Bus Routes in Downtown Seattle Application: Topological Sort Given a directed graph, G = (V,E), output all the in V such that no vertex is output before any other vertex with an edge to it. CSE CSE CSE 0 CSE CSE CSE 70 CSE CSE 78 CSE 6 CSE CSE 5 CSE 67 If we re at rd and Pine, how can we get to st and University using Metro? /6/00 7 Is the output unique? /6/00 8

Valid Topological Sorts: Topological Sort: Take One 0. Label each vertex with its in-degree (# of inbound edges). While there are remaining: a. Choose a vertex v of in-degree zero; output v b. Reduce the in-degree of all adjacent to v c. Remove v from the list of Runtime: 0 /6/00 0 void Graph::topsort(){ Vertex v, w; labeleachvertexwithitsin-degree(); for (int counter=0; counter < NUM_VERTICES; counter++){ v = findnewvertexofdegreezero(); v.topologicalnum = counter; for each w adjacent to v w.indegree--; /6/00 Topological Sort: Take Two. Label each vertex with its in-degree. Initialize a queue Q to contain all in-degree zero. While Q not empty a. v = Q.dequeue; output v b. Reduce the in-degree of all adjacent to v c. If new in-degree of any such vertex u is zero Q.enqueue(u) Runtime: Note: could use a stack, list, set, box, instead of a queue /6/00 void Graph::topsort(){ Queue q(num_vertices); int counter = 0; Vertex v, w; labeleachvertexwithitsin-degree(); q.makeempty(); for each vertex v if (v.indegree == 0) q.enqueue(v); intialize the queue while (!q.isempty()){ get a vertex with v = q.dequeue(); indegree 0 v.topologicalnum = ++counter; for each w adjacent to v if (--w.indegree == 0) insert new q.enqueue(w); eligible Runtime: /6/00 Graph Traversals Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! Must mark visited so you do not go into an infinite loop! Either can be used to determine connectivity: Is there a path between two given? Is the graph (weakly) connected? Which one: Uses a queue? Uses a stack? Always finds the shortest path (for unweighted graphs)? /6/00

Graph Connectivity Undirected graphs are connected if there is a path between any two Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two, ignoring direction The Shortest Path Problem Given a graph G, edge costs c i,j, and s and t in G, find the shortest path from s to t. For a path p = v 0 v v v k unweighted length of path p = k (a.k.a. length) weighted length of path p = i=0..k- c i,i+ (a.k.a cost) A complete graph has an edge between every pair of Path length equals path cost when? /6/00 5 /6/00 6 Single Source Shortest Paths (SSSP) Given a graph G, edge costs c i,j, and vertex s, find the shortest paths from s to all in G. All Pairs Shortest Paths (APSP) Given a graph G and edge costs c i,j, find the shortest paths between all pairs of in G. Is this harder or easier than the previous problem? Is this harder or easier than SSSP? Could we use SSSP as a subroutine to solve this? /6/00 7 /6/00 8 Variations of SSSP Applications Weighted vs. unweighted Directed vs undirected Cyclic vs. acyclic Positive weights only vs. negative weights allowed Shortest path vs. longest path Network routing Driving directions Cheap flight tickets Critical paths in project management (see textbook) /6/00 9 /6/00 0

Ideas? SSSP: Unweighted Version void Graph::unweighted (Vertex s){ Queue q(num_vertices); Vertex v, w; q.enqueue(s); s.dist = 0; /6/00 while (!q.isempty()){ v = q.dequeue(); for each w adjacent to v if (w.dist == INFINITY){ w.dist = v.dist + ; w.path = v; q.enqueue(w); total running time: O( ) each edge examined at most once if adjacency lists are used each vertex enqueued at most once /6/00