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

Slides:



Advertisements
Similar presentations
0 - 0.
Advertisements

MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Section 2.5: Graphs and Trees
Lecture 15. Graph Algorithms
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
Addition 1’s to 20.
Week 1.
CS203 Lecture 15.
Epp, section 10.? CS 202 Aaron Bloomfield
Analysis of Algorithms CS 477/677
CSE Lectures 18 – Graphs Graphs & Characteristics
CS16: Introduction to Data Structures & Algorithms
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.
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.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Graphs – ADTs and Implementations ORD DFW SFO LAX
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Graphs Chapter 12.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is NOT meant by term “Graph”  Why no bar charts, scatter plots, & pie charts mentioned.
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 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.
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
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.
Design and Analysis of Algorithms Introduction to graphs, representations of a graph Haidong Xue Summer 2012, at GSU.
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.
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
Data Structures and Algorithms for Information Processing
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 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 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.
Graphs G = (V, E) V are the vertices; E are the edges.
CSC 380: Design and Analysis of Algorithms
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 20: Graphs II

Implementing a Graph To program a graph data structure, what information would we need to store? For each vertex? For each edge?

Implementing a Graph What kinds of questions would we want to be able to answer (quickly?) about a graph G? Where is vertex v? Which vertices are adjacent to vertex v? What edges touch vertex v? What are the edges of G? What are the vertices of G? What is the degree of vertex v?

Graph Implementation Strategies Edge List Adjacency Matrix Adjacency List 4

Edge List 5 edge list: an unordered list of all edges in the graph * This is NOT an array

Edge List: Pros and Cons 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)

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

Adjacency Matrix: Pros and Cons 8 advantages fast to tell whether edge exists between any two vertices i and j (and to get its weight) disadvantages consumes a lot of memory on sparse graphs (ones with few edges) redundant information for undirected graphs

Adjacency Matrix Example 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?

Adjacency Lists 10 adjacency list: stores edges as individual linked lists of references to each vertex's neighbors

Adjacency List: Pros and Cons 11 advantages: new nodes can be added easily new nodes can be connected with existing nodes easily "who are my neighbors" easily answered disadvantages: determining whether an edge exists between two nodes: O(average degree)

Adjacency List Example 12 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?

Runtime table 13 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 adding a vertex adding an edge removing vertex v removing an edge n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Spacen + m n2n2 Finding all adjacent vertices to v mdeg(v)n Determining if v is adjacent to w mdeg(v)1 adding a vertex11n2n2 adding an edge to v111 removing vertex vmn?n?n2n2 removing an edge from vmdeg(v)1

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? 14 ORD PVD MIA DFW SFO LAX LGA HNL

Practical Implementation Adjacency list Each Vertex maps to a List of edges Vertex List To get all edges adjacent to v 1, look up List neighbors = map.get(v 1 ) 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 v 1 to v 2, call map.get(v 1 ).containsKey(v 2 ) To get the edge from v 1 to v 2, call map.get(v 1 ).get(v 2 ) 15

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

Edge class 17 public class Edge { 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 " "; }