CSE 373: Data Structures and Algorithms

Slides:



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

Lecture 15. Graph Algorithms
CS16: Introduction to Data Structures & Algorithms
Review Binary Search Trees Operations on Binary Search Tree
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
CSC 213 – Large Scale Programming Lecture 27: Graph ADT.
CS 410 Applied Algorithms Applied Algorithms Lecture #3 Data Structures.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
CSC 213 ORD DFW SFO LAX Lecture 20: Graphs.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Chapter 12.
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 A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
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.
1 Graphs Terminology By: Sandeep Tuli Astt. Prof. CSE.
Contest Algorithms January 2016 Introduce the main kinds of graphs, discuss two implementation approaches, and remind you about trees 6. Intro. to Graphs.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
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.
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
CMSC 341 Lecture 21 Graphs (Introduction)
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.
Shortest Paths C B A E D F Shortest Paths
CSE 373: Data Structures and Algorithms
Graph Operations And Representation
Graphs ORD SFO LAX DFW Graphs 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
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Algorithms: Design and Analysis
Algorithms: Design and Analysis
Graphs ORD SFO LAX DFW /15/ :57 AM
Graphs.
Graphs G = (V, E) V are the vertices; E are the edges.
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Graph Operations And Representation
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
Chapter 9 Graph algorithms
Heaps Chapter 6 Section 6.9.
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 20: Graphs II

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 its 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 2 3 4 5 6 7

Edge list edge list: an unordered list of all edges in the graph advantages easy to loop/iterate over all edges disadvantages hard to tell if an edge exists from A to B hard to tell how many edges a vertex touches (its degree) 1 2 3 4 5 6 7 1 2 5 6 7 3 4

Adjacency matrix adjacency matrix: an n × n matrix where: the nondiagonal entry aij 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 aii corresponds to the number of loops (self-connecting edges) at vertex i

Pros/cons of Adj. matrix advantage: fast to tell whether 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)

Adjacency matrix example The graph at right has the following adjacency matrix: How do we figure out the degree of a given vertex? How do we find out whether an edge exists from A to B? How could we look for loops in the graph? 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7

Adjacency lists adjacency list: stores edges as individual linked lists of references to each vertex's neighbors generally, no information needs to be stored in the edges, only in nodes, these arrays can simply be pointers to other nodes and thus represent edges with little memory requirement

Pros/cons of adjacency list advantage: new nodes can be added to the graph easily, and they can be connected with existing nodes simply by adding elements to the appropriate arrays; "who are my neighbors" easily answered disadvantage: determining whether an edge exists between two nodes requires O(n) time, where n is the average number of incident edges per node

Adjacency list example The graph at right has the following adjacency list: How do we figure out the degree of a given vertex? How do we find out whether an edge exists from A to B? How could we look for loops in the graph? 1 2 3 4 5 6 7 1 2 3 4 5 6 7

Runtime table n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Space n + m n2 Finding all adjacent vertices to v m deg(v) n Determining if v is adjacent to w 1 inserting a vertex inserting an edge removing vertex v removing an edge n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Space Finding all adjacent vertices to v Determining if v is adjacent to w inserting a vertex inserting an edge removing vertex v removing an edge

Practical implementation Not all graphs have vertices/edges that are easily "numbered" how do we actually represent 'lists' or 'matrices' of vertex/edge relationships? How do we quickly look up the edges and/or vertices adjacent to a given vertex? Adjacency list: Map<V, List<V>> Adjacency matrix: Map<V, Map<V, E>> ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1 2 3 4 5 6 7 1

Maps and sets within graphs since not all vertices can be numbered, we can use: 1. adjacency list each Vertex maps to a List of edges Vertex --> List of Edges to get all edges adjacent to V1, look up List<Edge> neighbors = map.get(V1) 2. adjacency map (adjacency matrix for objects) each Vertex maps to a hashtable of adjacent vertices Vertex --> (Vertex --> Edge) to find out whether there's an edge from V1 to V2, call map.get(V1).containsKey(V2) to get the edge from V1 to V2, call map.get(V1).get(V2)

Implementing Graph with Adjacency List public interface Graph<V> { public void addVertex(V v); public void addEdge(V v1, V v2, int weight); public boolean hasEdge(V v1, V v2); public Edge<V> getEdge(V v1, V v2); public boolean hasPath(V v1, V v2); public List<V> getDFSPath(V v1, V v2); public String toString(); }

Edge class public class Edge<V> { public V from, to; public int weight; public Edge(V from, V to, int weight) { if (from == null || to == null) { throw new IllegalArgumentException("null"); } this.from = from; this.to = to; this.weight = weight; public String toString() { return "<" + from + ", " + to + ", " + weight + ">";