Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Analysis of Algorithms CS 477/677
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
Chapter 9: Graphs Topological Sort
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Data Structures & Algorithms 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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs and Shortest Paths Using ADTs and generic programming.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Graphs Lecture 19 CS2110 – Spring 2013.
CSE 373 Topological Sort Graph Traversals
Lecture 11 Graph Algorithms
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph.
Refresh and Get Ready for More
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Connected Components, Directed Graphs, Topological Sort
CSCI2100 Data Structures Tutorial
Graphs G = (V, E) V are the vertices; E are the edges.
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Graphs G = (V,E) V is the vertex set.
Presentation transcript:

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction Chapter 9 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Motivation Several real-life problems can be converted to problems on graphs Graphs are one of the pervasive data structures used in computer science Graphs are a useful tool for modeling real-world problems Graphs allow us to abstract details and focus on the problem We can represent our domain as graphs and apply graph algorithms to solve our problem

Examples Given a map of WSU and the surrounding area, how to get from one place to another? A B D C E F G H J I

Examples (cont’d) What data structure to use to represent the problem? How do you even think about the problem? A B D C E F G H J I

Examples (cont’d) Let us strip away irrelevant details We have a set of vertices {A, B, C, D, E, F, G, H, I, J} A B D C E F G H J I

Examples (cont’d) Let us strip away irrelevant details We have a set of edges connecting the vertices A B D C E F G H J I

Examples (cont’d) Let us strip away irrelevant details Edges can be assigned weights A 6 B 5 D 7 3 C E F 4 9 6 6 8 G H J 2 I

Examples (cont’d) Let us strip away irrelevant details A 6 B 5 D 7 3 C F 4 9 6 6 8 G H J 2 I

Other Examples Protein-protein interaction Internet Social network Power grid WWW

G = (V, E) Simple Graphs V is a set of vertices. E is a set of edges. u u  V, v  V (u, v)  E v

G = (V, E) Directed Graphs V is a set of vertices. E is a set of edges. u u  V, v  V (u, v)  E (v, u)  E v

G = (V, E) Weighted Graphs V is a set of vertices E is a set of edges 6 5 V is a set of vertices E is a set of edges 7 3 4 9 6 8 J 2

Definition of a Graph A graph G = (V, E) consists of a set of vertices V and a set of edges E E = {(u, v} | u, v  V} Vertex v is adjacent to vertex u Edges are sometimes called arcs Example V = {A, B, C, D, E, F, G} E = {(A, B), (A, D), (B, C), (C, D), (C, G), (D, E), (D, F), (E, F)}

Definitions Undirected graphs Directed graphs (digraphs) Edges are unordered (i.e. (u, v) is the same as (v, u)) Directed graphs (digraphs) Edges are ordered (i.e. <u, v>  <v, u>) Weighted graphs Edges have a weight w(u, v) or cost (u, v)

Definitions (cont’d) Degree of a vertex Indegree Outdegree Number of edges incident on a vertex Indegree Number of directed edges to vertex Outdegree Number of directed edges from vertex degree(v4) = 6 indegree(v4) = 3 outdegree(v4) = 3 indegree(v1) = 0 outdegree(v1) = 3 indegree(v6) = 3 outdegree(v6) = 0

Definitions (cont’d) Path Cycle Sequence of vertices v1, v2, …, vN such that (vi, vi + 1)  E for 1  i  N Path length is the number of edges on the path (i.e., N – 1) Simple path has unique intermediate vertices Cycle Path where v1 = vN Usually simple and directed Acyclic graphs have no cycles

Definitions (cont’d) Undirected graph is connected if there is a path between every pair of vertices Connected, directed graph is called strongly connected Complete graph has an edge between every two vertices v6 to v1, v7 to v1

Representations Adjacency matrix is a two dimensional array For each edge (u,v), A[u][v] is true otherwise it is false

Representations (cont’d) It is good to use adjacency lists for sparse graphs Sparse means not dense Graph is dense means |E| = (|V|2)

Practical Problem Representations Graph represents a street map with Manhattan-like orientation Streets run mainly on north-south or east-west Any intersection is attached with four streets Graph is directed and all streets are two way, then |E| = 4|V| Example: Assume 3000 intersections = 3000-vertex graph and 12000 edges Array size for Adjacency Matrix = 9,000,000 Most of these entries would contain 0 If the graph is sparse a better solution is adjacency list For each vertex we keep a list of all adjacent vertices Space requirement is O(|E|+|V|), linear in the size of the graph

Graph Algorithms: Topological Sort

Topological Sort Order the vertices in a directed acyclic graph (DAG), such that if (u, v)  E, then u appears before v in the ordering Example Course Prerequisite Structure at a University

Topological Sort (cont’d) Topological ordering is not possible if the graph has a cycle, since for two vertices u and v on the cycle, u precedes v and v precedes u The ordering is not necessarily unique; any legal ordering will do Possible topological orderings: v1, v2, v5, v4, v3, v7, v6 and v1, v2, v5, v4, v7, v3, v6.

Topological Sort (cont’d) Solution #1 While there are vertices left in the graph Find vertex v with indegree equals to 0 Output v Remove v from the graph together with all edges to and from v Running of Solution #1 is O(|V|2) Possible topological ordering: v1, v2, v5, v4, v3, v7, v6.

Topological Sort Pseudocode //Not been assigned a topological number Takes O(|V|) times, |V| such calls CycleFoundException()

Topological Sort (cont’d) Solution #2 Don’t need to search over all vertices for indegree = 0 Only vertices that lost an edge from the previous vertex’s removal need to be searched Algorithm Compute the indegree for every vertex Place all vertices of indegree 0 to an initially empty queue (note: we can also use a stack) While the queue is not empty Remove a vertex v from the queue Output v Decrement indegrees of all vertices adjacent to v Put a vertex on the queue as soon as its indegree falls to 0

Topological Sort (cont’d)

Topological Sort Pseudocode

Topological Sort (cont’d) Solution #2 Assume that the graph is already read into an adjacency list Assume the indegrees are computed and stored with the vertices Running time of Solution #2 is O(|V| + |E|) Possible topological ordering: v1, v2, v5, v4, v3, v7, v6.

Graph Algorithms Topological sort Shortest paths Network flow Minimum spanning tree Applications NP-Completeness