Data Structures and Analysis (COMP 410)

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CSCI-455/552 Introduction to High Performance Computing Lecture 18.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Representing and Using Graphs
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
Data Structures & Algorithms Graphs
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
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.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
abstract data types built on other ADTs
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.
Shortest Paths.
Data Structures and Analysis (COMP 410)
CS 201: Design and Analysis of Algorithms
CS212: Data Structures and Algorithms
Graphs Representation, BFS, DFS
Introduction to Graphs
Data Structures, Algorithms & Complexity
Lecture 11 Graph Algorithms
CS 367 – Introduction to Data Structures
Introduction to Graphs
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
CS 3343: Analysis of Algorithms
Data Structures and Analysis (COMP 410)
CMSC 341 Lecture 21 Graphs (Introduction)
Discrete Maths 9. Graphs Objective
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Assignment 7 questions?.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs Chapter 11 Objectives Upon completion you will be able to:
Search Related Algorithms
Dijkstra Algorithm.
Graphs.
Data Structures and Analysis (COMP 410)
ITEC 2620M Introduction to Data Structures
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
CSC 380: Design and Analysis of Algorithms
GRAPHS Lecture 17 CS2110 Spring 2018.
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Some Graph Algorithms.
GRAPHS Lecture 17 CS 2110 — Spring 2019.
Algorithm Course Dr. Aref Rashad
Lecture 10 Graph Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Introduction to Graphs
For Friday Read chapter 9, sections 2-3 No homework
Introduction to Graphs
Presentation transcript:

Data Structures and Analysis (COMP 410) David Stotts Computer Science Department UNC Chapel Hill

Modeling with Graphs, Implementing Graphs (part 2)

Finite State Machine Automaton drawn as labeled directed graph A F C h k E , Token Represents current state Simple abstract “computer” Recognizes and generate strings (languages) Used in compilers, keyword tokenizers COMP 455 models of languages and computation g a c c c k , a r r r r g h , a r g h , g a c c k , g a c c c c c k , a r r g h

Predicate Transition Nets Automaton that is represented with bipartite graph is bipartite p1 p3 p5 t1 p4 t2 p2 t3 t4 t5 t6 Token Represents locus of activity

Modeling with Graphs Example: air routes, road maps Weighted edges bost seatt 20 70 nyc chi 30 10 20 50 30 10 5 denv 50 sfr 20 70 dc 25 10 10 40 40 30 20 char 15 20 20 la 90 dall 25 atl Weighted edges

Modeling Weights can be “cost” Directed edges miles, speed, fuel, time, total $$, wire/fiber, etc. Directed edges cost might be less one way than another jet stream, might be faster west than east different planes on different routes uploads copper, downloads satellite Add weights to get total cost of a route A to B Add #hops to get other solutions #take off/landings = tire wear, maint hours, taxes, risk

Modeling What is min cost NYC to LA? NYC to LA min hops? NYC –30> Chi –50> Denv –40> LA 3 hops, cost is 120 NYC –5> DC –70> Denv –40> LA 3 hops, cost is 115 NYC –30> Chi –70> Seatt –20> SFr –10> LA 4 hops, cost is 130 NYC –5> DC –10> Char –30> Dall –20> Denv –40> LA 5 hops, cost is 105 Can I get from DC to any other city? Is the map strongly connected? If so, then yes

Representing Graphs Adjacency Matrix (unweighted edges) For N nodes, we use an NxN array of Boolean if AM[a,b]==True then (a,b)∈E A B C D A B C D s o u r c e destination A C B D F T T T T T

Representing Graphs Weighted Edges Array elements can be integer, real, etc. A B C D A B C D s o u r c e destination A C B D 2 3 4 6 3 6 2 2 4

Adjacency Matrix Problem is wasted space O( 𝑵 𝟐 ) space If |E|≪ 𝑵 𝟐 lots of wasted space 8 nodes T 64 spaces, 55 not used If |E|≈ |𝑽| 𝟐 we call the graph dense Then adjacency matrix is space efficient If |E|≈ |𝑽| 𝟐 we say graph is O( |𝑽| 𝟐 ) in space. If |E|≪ |𝑽| 𝟐 we call the graph sparse Then adjacency matrix is not space efficient Sparse can be surprising, denser than it sounds

Sparse? Let’s model traffic in Manhattan Let V be intersections of streets Let E be streets between V Assume 2-way, use digraph Graph seems complex… is it dense? Each node has 4 edges out (non side nodes) So |E| ≈ 𝟒∗|𝑽| for large V, |E|≪ |𝑽| 𝟐 Say we have 3000 intersections in Manhattan Matrix has 𝟑𝟎𝟎𝟎 𝟐 cells, or 9,000,000 cells Cells used ≈ 4*3000, or 12,000 cells used This leaves 8,988,000 cells not used Our traffic flow graph is NOT dense, it is sparse

Adjacency Matrix Summary Easy to build and code Usually wastes most of its space When a graph is sparse, finding adjacent nodes is expensive Given vertex v, find all vertices adjacent to it Go to row v in matrix O(|V|) operation to look at all columns in that row Only find a few vertices (graph is sparse) Manhattan example: O(3000) looks to find 4 vertices

Dense? Or Sparse? so… Sparse 1538 nodes, 8032 edges (1538 nodes)^2 is 2365444 8032 edges ≪ 2365444 so… Sparse From Jzy3d plotting software website

Complete Graphs are Dense K100 K16 Effluviamagazine.com/effluviablog Friday, June 27, 2014 from numerics.io

Dense? Or Sparse? from Martin Grandjean on Digital Humanities site

Adjacency List A better approach for sparse graphs Keep list of vertices Each vertex has a list of adjacent vertices List of lists HashMap of lists Array of lists A B D C B C D A C B D B C

Adjacency List If edges are weighted store that in the link cell A C B 2 3 4 6 A 3 6 2 B C 2 D 4

Another Example Array for nodes, linked cells for edges 1 A C B D 3 5 2 E 6 4 A B D C 1 2 3 4 E 2 1 3 4 1 2 3 5 1 6 4 1

Adjacency List Space needed: O( |V|+|E| ) We call this “linear” for graphs So “size” of a graph is |V|+|E| Find all vertices adjacent to some node v ? Worst case O(|V|) (complete graph) Normally O( adj list for v ) Adj Matrix gives O(|V|) every search, worst and avg

Efficiency You will need a hashmap for vertices Hash each vertex name to the vertex object Many graph algorithms will be inefficient without If you need to find a vertex, you cannot afford to do O(|V|) search through an “all vertices” list without making many algorithms become quadratic, or worse You will still want the vertices linked, for times you need to visit every vertex systematically May need similar hash structure for edges

END