CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.

Slides:



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

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
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.
Chapter 9: Graphs Basic Concepts
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
CSE332: Data Structures & Algorithms Lecture 13: Introduction to Graphs Dan Grossman Fall 2013.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
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.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE332: Data Structures & Algorithms Lecture 14: Introduction to Graphs Aaron Bauer Winter 2014.
Graphs and Shortest Paths Using ADTs and generic programming.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
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.
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.
Introduction (with applications) ADT & terminology
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
CS120 Graphs.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Graphs Graph transversals.
Paul Beame in lieu of Richard Anderson
CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS
Chapter 9: Graphs Basic Concepts
Graph Operations And Representation
Directed Graph Algorithms
Directed Graph Algorithms
Based on slides of Dan Suciu
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 9: Graphs Basic Concepts
Presentation transcript:

CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001

Today’s Outline Project IV Stuff Bart didn’t get to Monday Graphs (what are they?) Topological Sort Graph Data Structures Graph Properties

Graph… ADT? a graph G is represented as G = (V, E) –V is a set of vertices: {v 1, v 2, …, v n } –E is a set of edges: {e 1, e 2, …, e m } where each e i connects two vertices (v i1, v i2 ) operations include: –iterating over vertices –iterating over edges –iterating over vertices adjacent to a specific vertex –asking whether an edge exists connected two vertices Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)} Graphs - a formalism for representing relationships

How Many Edges? A DC E B

Graph Applications Storing things that are graphs by nature –distance between cities –airline flights, travel options –relationships between people, things –distances between rooms in Clue Compilers –callgraph - which functions call which others –dependence graphs - which variables are defined and used at which statements

Total Order AB means A must go before B

Partial Order: Planning a Trip check in airport call taxi taxi to airport reserve flight pack bags take flight locate gate

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 1.Pick a vertex with in-degree of zero and output it 2.Reduce the in-degree of all vertices adjacent to it 3.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 1.Pick a vertex v with in-degree of zero and output it 2.Reduce the in-degree of all vertices adjacent to v 3.Put any of these with new in-degree zero in the queue 4.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:

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 Directed vs. Undirected Graphs Han Leia Luke Han Leia Luke aka: di-graphs

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 = {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.

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

To Do Finish Project III (due Today!) Read chapter 9 (see Calendar) Read Project IV writeup

Coming Up Graph Algorithms! Quiz (tomorrow) Project IV code