CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms Lecture 20: Graphs II.
Advertisements

CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
CSC 213 – Large Scale Programming Lecture 27: Graph ADT.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Subchap 22.1 – Representation of Graphs.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Graphs Chapter 12.
CSC 213 – Large Scale Programming. Today’s Goals  Briefly review graphs and vital graph terms  Begin discussion of how to implement Graph  Vertex &
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Spring 2007Graphs1 ORD DFW SFO LAX
CSE 373: Data Structures and Algorithms
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.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
CSE 373 Data Structures and Algorithms
Graphs.
Graphs 5/14/ :46 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Chapter 28 Graphs and Applications
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Graphs Algorithm Design and Analysis Bibliography:
Graphs.
Graphs 7/18/2018 7:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW Graphs Graphs
Unit 3 Graphs.
CS120 Graphs.
Graphs Part 1.
CMSC 341 Lecture 21 Graphs (Introduction)
CSE 373: Data Structures and Algorithms
Discrete Maths 9. Graphs Objective
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs.
Graphs.
Graph Operations And Representation
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs CSE 2011 Winter November 2018.
CSE 373: Data Structures and Algorithms
Graph Operations And Representation
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Algorithms: Design and Analysis
Algorithms: Design and Analysis
Graphs ORD SFO LAX DFW /15/ :57 AM
Graphs.
CSC 380: Design and Analysis of Algorithms
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 16 1 – Graphs Graph Categories Strong Components
Graphs.
Graphs.
CSC 380: Design and Analysis of Algorithms
Graph Operations And Representation
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graphs.
Graphs Algorithm Design and Analysis Bibliography:
Chapter 9 Graph algorithms
Presentation transcript:

CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9 slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved.

Implementing a graph If we wanted to program an actual data structure to represent a graph, what information would we need to store? for each vertex? for each edge? What kinds of questions would we want to be able to answer quickly: about a vertex? about edges / neighbors? about paths? about what edges exist in the graph? We'll explore three common graph implementation strategies: edge list, adjacency list, adjacency matrix 1 4 2 7 3 5 6

Edge list edge list: An unordered list of all edges in the graph. an array, array list, or linked list advantages: easy to loop/iterate over all edges disadvantages: hard to quickly tell if an edge exists from vertex A to B hard to quickly find the degree of a vertex (how many edges touch it) 1 4 2 7 3 5 6 1 2 3 4 5 6 7 8 (1, 2) (1, 4) (1, 7) (2, 3) 2, 5) (3, 6) (4, 7) (5, 6) (6, 7)

Graph operations Using an edge list, how would you find: all neighbors of a given vertex? the degree of a given vertex? whether there is an edge from A to B? whether there are any loops (self-edges)? What is the Big-Oh of each operation? 1 4 2 7 3 5 6 1 2 3 4 5 6 7 8 (1, 2) (1, 4) (1, 7) (2, 3) 2, 5) (3, 6) (4, 7) (5, 6) (6, 7)

Adjacency matrix adjacency matrix: An N × N matrix where: the non-diagonal entry a[i,j] is the number of edges joining vertex i and vertex j (or the weight of the edge joining vertex i and vertex j). the diagonal entry a[i,i] corresponds to the number of loops (self-connecting edges) at vertex i (often disallowed). in an undirected graph, a[i,j] = a[j,i] for all i, j. (diagonally symmetric) 1 2 3 4 5 6 7 1 4 2 7 3 5 6

Graph operations Using an adjacency matrix, how would you find: all neighbors of a given vertex? the degree of a given vertex? whether there is an edge from A to B? whether there are any loops (self-edges)? What is the Big-Oh of each operation? 1 2 3 4 5 6 7 1 4 2 7 3 5 6

Adj matrix pros / cons advantages: disadvantage: fast to tell whether an edge exists between any two vertices i and j (and to get its weight) disadvantage: consumes a lot of memory on sparse graphs (ones with few edges) 1 2 3 4 5 6 7 1 4 2 7 3 5 6

Adjacency list adjacency list: Stores edges as individual linked lists of references to each vertex's neighbors. in unweighted graphs, the lists can simply be references to other vertices and thus use little memory in undirected graphs, edge (i, j) is stored in both i's and j's lists 1 2 3 4 5 6 7 2 4 7 1 3 5 6 1 4 2 7 3 5 6

Graph operations Using an adjacency list, how would you find: all neighbors of a given vertex? the degree of a given vertex? whether there is an edge from A to B? whether there are any loops (self-edges)? What is the Big-Oh of each operation? 1 2 3 4 5 6 7 2 4 7 1 3 5 6 1 4 2 7 3 5 6

Adj list pros / cons advantages: disadvantages: new vertices can be added to the graph easily, and they can be connected with existing nodes simply by adding elements to the appropriate arrays; easy to find all neighbors of a given vertex (and its degree) disadvantages: determining whether an edge exists between two vertices requires O(N) time, where N is the average number of edges per node

Weighted/directed graphs adj. list: store weight in each edge node adj. matrix: store weight in each matrix box directed: adj. list: edges appear only in start vertex's list adj. matrix: no longer diagonally symmetric 2 1 4 7 3 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 2:3 4:5 3:6 5:1 6:1 7:6 2:1 6:2 3:2 7:4 1:2 4:6 5:3

Runtime comparison |V| vertices, |E| edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Memory usage |V| + |E| |V|2 Find all neighbors of v |E| degree(v) |V| Is v a neighbor of w? 1 add a vertex add an edge remove a vertex remove an edge deg(v)

Representing vertices Not all graphs have vertices/edges that are easily "numbered". How do we represent lists or matrices of vertex/edge relationships? How do we quickly look up edges or vertices near a given vertex? edge list: List<Edge> adjacency list: Map<Vertex, List<Edge>> or Multimap<Vertex, Edge> adjacency matrix: Map<Vertex, Map<Vertex, Edge>> or Table<Vertex, Vertex, Edge> ORD PVD MIA DFW SFO LAX LGA HNL $50 $80 $140 $170 $70 $100 $110 $120 $60 $250 $200 $500 $130

A graph ADT As with other ADTs, we can create a Graph ADT interface: public interface Graph<V, E> { void addEdge(V v1, V v2, E e, int weight); void addVertex(V v); void clear(); boolean containsEdge(E e); boolean containsEdge(V v1, V v2); boolean containsVertex(V v); int cost(List<V> path); int degree(V v); E edge(V v1, V v2); int edgeCount(); Set<E> edges(); int edgeWeight(V v1, V v2); int inDegree(V v); ...

A graph ADT, cont'd. // public interface Graph<V, E> { ... boolean isDirected(); boolean isEmpty(); boolean isReachable(V v1, V v2); // DFS boolean isWeighted(); List<V> minimumWeightPath(V v); // Dijkstra's Set<V> neighbors(V v); int outDegree(V v); void removeEdge(V v1, V v2); void removeVertex(V v); List<V> shortestPath(V v1, V v2); // BFS String toString(); int vertexCount(); Set<V> vertices(); }

Info about vertices Information stored in each vertex (for internal use): can store various flags and fields for use by path search algorithms public class Vertex<V> { public int cost() {...} public int number() {...} public V previous() {...} public boolean visited() {...} public void setCost(int cost) {...} public void setNumber(int number) {...} public void setPrevious(V previous) {...} public void setVisited(boolean visited) {...} public void clear() {...} // reset dist,prev,visited }

Info about edges Information stored in each edge (for internal use): public class Edge<V, E> { public boolean contains(V vertex) {...} public E edge() {...} public V end() {...} public V start() {...} public int weight() {...} // 1 if unweighted }