Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Advertisements

Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
COSC 2007 Data Structures II
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs.
CS 201: Design and Analysis of Algorithms
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
CS212: Data Structures and Algorithms
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Csc 2720 Instructor: Zhuojun Duan
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
Introduction to Graphs
Depth-First Search.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
Data Structures and Algorithms for Information Processing
CMSC 341 Lecture 21 Graphs (Introduction)
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Data Structures and Algorithms
Graphs Graph transversals.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Data Structures and Algorithms
Graphs CSE 2011 Winter November 2018.
Can you get there from here?
Chapter 22: Elementary Graph Algorithms I
Graphs.
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Data Structures and Algorithms
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Lecture 13 CSE 331 Sep 27, 2017.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Depth-First Search CSE 2011 Winter April 2019.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Graphs.
Graphs.
Graphs.
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graphs.
Analysis and design of algorithm
Lecture 10 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Lecture 20: Implementing Graphs
Lecture 27: More Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Data Structures and Algorithms Implementing Graphs Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion https://visualdatahub.wordpress.com/2013/04/02/a-web-of-thrones-character-relationships/

Introduction to Graphs CSE 373 SP 18 - Kasey Champion

Inter-data Relationships Arrays Categorically associated Sometimes ordered Typically independent Elements only store pure data, no connection info Trees Directional Relationships Ordered for easy access Limited connections Elements store data and connection info Graphs Multiple relationship connections Relationships dictate structure Connection freedom! Both elements and connections can store data 1 2 A B C A B C B C A CSE 373 SP 18 - Kasey Champion

Graph: Formal Definition B C D E F G H A graph is defined by a pair of sets G = (V, E) where… V is a set of vertices A vertex or “node” is a data entity E is a set of edges An edge is a connection between two vertices V = { A, B, C, D, E, F, G, H } E = { (A, B), (A, C), (A, D), (A, H), (C, B), (B, D), (D, E), (D, F), (F, G), (G, H)} CSE 373 SP 18 - Kasey Champion

Applications Physical Maps Relationships Influence Related topics Airline maps Vertices are airports, edges are flight paths Traffic Vertices are addresses, edges are streets Relationships Social media graphs Vertices are accounts, edges are follower relationships Code bases Vertices are classes, edges are usage Influence Biology Vertices are cancer cell destinations, edges are migration paths Related topics Web Page Ranking Vertices are web pages, edges are hyperlinks Wikipedia Vertices are articles, edges are links SO MANY MORREEEE www.allthingsgraphed.com CSE 373 SP 18 - Kasey Champion

Graph Vocabulary Graph Direction Degree of a Vertex Undirected Graph: Undirected graph – edges have no direction and are two-way Directed graphs – edges have direction and are thus one-way Degree of a Vertex Degree – the number of edges connected to that vertex Drogo : 1, Danny : 1, Jon : 1 In-degree – the number of directed edges that point to a vertex Petyr : 0, Catelyn : 2, Ned : 1 Out-degree – the number of directed edges that start at a vertex Petyr : 1, Catelyn : 1, Ned : 1 Drogo Dany Jon V = { Dany, Drogo, Jon } E = { (Dany, Drogo), (Dany, Jon) } inferred (Drogo, Dany) and (Jon, Dany) Directed Graph: V = { Petyr, Catelyn, Ned } Petyr Catelyn Ned E = { (Petyr, Catelyn), (Catelyn, Ned), (Ned, Catelyn) } CSE 373 SP 18 - Kasey Champion

Graph Vocabulary Self loop – an edge that starts and ends at the same vertex Parallel edges – two edges with the same start and end vertices Simple graph – a graph with no self-loops and no parallel edges Petyr Dany Drogo Loras Brienne Margaery Renly CSE 373 SP 18 - Kasey Champion

Food for thought Is a graph valid if there exists a vertex with a degree of 0? Yes Robert Lyanna Rheagar Petyr Catelyn Ned Cersei Jaime Robert Petyr has an “in degree” of 0 Lyanna has an “out degree” of 0 Robert has both an “in degree” and an “out degree” of 0 Are these valid? Is this a valid graph? Yup Stark Lannister Tyrells Targaryens Robert Joffrey Tommen Cersei Jon Sure Yes! CSE 373 SP 18 - Kasey Champion

Implementing a Graph Implement with nodes… Implementation gets super messy What if you wanted a vertex without an edge? How can we implement without requiring edges to access nodes? Implement using some of our existing data structures! CSE 373 SP 18 - Kasey Champion

Adjacency Matrix Assign each vertex a number from 0 to V – 1 Create a V x V array of Booleans If (x,y) ∈ E then arr[x][y] = true Runtime (in terms of V and E) get out - edges for a vertex O(v) get in – edges for a vertex O(v) decide if an edge exists O(1) insert an edge O(1) delete an edge O(1) delete a vertex (subject to implementation) add a vertex (subject to implementation) How much space is used? V2 A B C D CSE 373 SP 18 - Kasey Champion

Graph Vocabulary Dense Graph – a graph with a lot of edges E ∈ Θ(V2) Stark Lannister Tyrells Targaryens Dense Graph – a graph with a lot of edges E ∈ Θ(V2) Sparse Graph – a graph with “few” edges E ∈ Θ(V) An Adjacency Matrix seems a waste for a sparse graph… Sam Jon Yoren Pyp Eddison CSE 373 SP 18 - Kasey Champion

Adjacency List A B C D Create a Dictionary of size V from type V to Collection of E If (x,y) ∈ E then add y to the set associated with the key x Runtime (in terms of V and E) get out - edges for a vertex O(1) get in - edges for a vertex O(V + E) decide if an edge exists O(1) insert an edge O(1) delete an edge O(1) delete a vertex (subject to implementation) add a vertex (subject to implementation) How much space is used? V + E 1 2 3 A B C B C B D D A CSE 373 SP 18 - Kasey Champion

Walks and Paths Walk – continuous set of edges leading from vertex to vertex A list of vertices where if I is some int where 0 < 1 < Vn every pair (Vi, Vi+1) in E is true Path – a walk that never visits the same vertex twice Winterfell Castle Black King’s Landing Casterly Rock Winterfell Castle Black King’s Landing Casterly Rock CSE 373 SP 18 - Kasey Champion

Connected Graphs Connected graph – a graph where every vertex is connected to every other vertex via some path. It is not required for every vertex to have an edge to every other vertex There exists some way to get from each vertex to every other vertex Connected Component – a subgraph in which any two vertices are connected via some path, but is connected to no additional vertices in the supergraph There exists some way to get from each vertex within the connected component to every other vertex in the connected component A vertex with no edges is itself a connected component Sansa Robb Bran Arya Rickon Dany Viserys Jon CSE 373 SP 18 - Kasey Champion

Graph Algorithms CSE 373 SP 18 - Kasey Champion

Traversing a Graph In all previous data structures: Start at first element Move to next element Repeat until end of elements For graphs – Where do we start? How do we decide where to go next? When do we end? Pick any vertex to start, mark it “visited” Put all neighbors of first vertex in a “to be visited” collection Move onto next vertex in “to be visited” collection Mark vertex “visited” Put all unvisited neighbors in “to be visited” Repeat… CSE 373 SP 18 - Kasey Champion

Breadth First Search Current node: Queue: Visited: search(graph) toVisit.enqueue(first vertex) while(toVisit is not empty) current = toVisit.dequeue() for (v : current.neighbors()) if (v is not in visited) toVisit.enqueue(v) visited.add(current) F G D A I H E B Current node: Queue: Visited: A D E F B C I H G G C B D E C F G H I A B D E C F G H I CSE 373 SP 18 - Kasey Champion

Breadth First Search Analysis search(graph) toVisit.enqueue(first vertex) while(toVisit is not empty) current = toVisit.dequeue() for (v : current.neighbors()) if (v is not in visited) toVisit.enqueue(v) visited.add(current) F G D A I H E B Visited: A B D E C F G H I C How many times do you visit each node? How many times do you traverse each edge? 1 time each Max 2 times each Putting them into toVisit Checking if they’re in toVisit Runtime? O(V + 2E) = O(V + E) “graph linear” CSE 373 SP 18 - Kasey Champion

Depth First Search (DFS) BFS uses a queue to order which vertex we move to next Gives us a growing “frontier” movement across graph Can you move in a different pattern? Can you use a different data structure? What if you used a stack instead? bfs(graph) toVisit.enqueue(first vertex) while(toVisit is not empty) current = toVisit.dequeue() for (V : current.neighbors()) if (V is not in visited) toVisit.enqueue(v) visited.add(current) dfs(graph) toVisit.push(first vertex) while(toVisit is not empty) current = toVisit.pop() for (V : current.neighbors()) if (V is not in visited) toVisit.push(v) visited.add(current) CSE 373 SP 18 - Kasey Champion

Depth First Search Current node: Stack: Visited: dfs(graph) toVisit.push(first vertex) while(toVisit is not empty) current = toVisit.pop() for (V : current.neighbors()) if (V is not in stack) toVisit.push(v) visited.add(current) F G D A I H E B Current node: Stack: Visited: C F I D A G H B E D C B E I H G F C A B E H G F I C D How many times do you visit each node? How many times do you traverse each edge? 1 time each Max 2 times each Putting them into toVisit Checking if they’re in toVisit Runtime? O(V + 2E) = O(V + E) “graph linear” CSE 373 SP 18 - Kasey Champion