1 Graphs Algorithms Sections 9.1, 9.2, and 9.3. 2 Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Shortest Paths On weighted graphs. Weighted Shortest Paths The shortest path from a vertex u to a vertex v in a graph is a path w 1 = u, w 2,…,w n = v,
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
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.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
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.
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 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
Chapter 2 Graph Algorithms.
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.
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.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Chapter 05 Introduction to Graph And Search Algorithms.
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.
Graphs and Shortest Paths Using ADTs and generic programming.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
CSE 373 Topological Sort Graph Traversals
Introduction to Graphs
Graphs.
Graph Algorithm.
Graphs Lecture 18 CS2110 – Fall 2009.
CS223 Advanced Data Structures and Algorithms
Chapter 11 Graphs.
Shortest Path Algorithms
ITEC 2620M Introduction to Data Structures
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
CE 221 Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
GRAPH – Definitions A graph G = (V, E) consists of
Introduction to Graphs
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:

1 Graphs Algorithms Sections 9.1, 9.2, and 9.3

2 Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set of edges (links) Complete graph –There is an edge between every pair of vertices Two kinds of graph –Undirected –Directed (digraph) Undirected graph: –E consists of sets of two elements each: Edge {u, v} is the same as {v, u}

3 Directed Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A directed graph, or digraph: –E is set of ordered pairs –Even if edge (u, v) is present, the edge (v, u) may be absent Directed graphs are drawn with nodes for vertices and arrows for edges

4 Terminology Adjacency –Vertex w is adjacent to v if and only (v, w) is in E Weight –A cost parameter associated with each edge Path –A sequence of vertices w 1,w 2,…,w n, where there is an edge for each pair of consecutive vertices Length of a path –Number of edges along path –Length of path of n vertices is n-1 Cost of a path –sum of the weights of the edges along the path v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v

5 Cycles A path is simple –if all its vertices are distinct (except that the first and last may be equal) A cycle is a path w 1,w 2,…,w n =w 1, –A cycle is simple if the path is simple. –Above, v 2, v 8, v 6, v 3, v 5, v 2 is a simple cycle in the undirected graph, but not even a path in the digraph v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4

6 Further terminology An undirected graph G is connected if, –for each pair of vertices u, v, there is a path that starts at u and ends at v A digraph H that satisfies the above condition is strongly connected Otherwise, if H is not strongly connected, but the undirected graph G with the same set of vertices and edges is connected, H is said to be weakly connected v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4

7 Representation of Graphs Two popular representations –Adjacency matrix –Adjacency list Adjacency matrix –A[N][N] – O(N 2 ) space –A[u][v] is true if there is an edge from u to v –False otherwise –For a weighted graph, assign weight instead of true/false –Wasteful if the graph is sparse (not many edges) Adjacency list –Each node maintains a list of neighbors (adjacent nodes) –Takes O(|V|+|E|) space Approach 1: A Hashtable maps vertices to adjacency lists Approach 2: Vertex structure maintains a list of pointers to other vertices

8 Representations of Graphs Adjacency list FTTTFFF FFFTTFF FFFFFTF FFTFFTT FFFTFFT FFFFFFF FFFFFTF Adjacency matrix

9 Topological sorting Let G be a directed acyclic graph (DAG) Topological sorting –an ordering the vertices of G such that if there is an edge from v i to v j, then v j appears after v i – One topological sorting MAC3311, COP3210, MAD2104, CAP3700, COP3400, COP3337, COP4555, MAD3305, MAD3512, COP3530, CDA4101, COP4610, CDA4400, COP4225, CIS4610, COP5621, COP4540

10 Topological sorting In a DAG, there must be a vertex with no incoming edges Have each vertex maintain its indegree –Indegree of v = number of edges (u, v) Repeat –Find a vertex of current indegree 0, –assign it a rank, –reduce the indegrees of the vertices in its adjacency list Running time = O(|V| 2 )

11 Topological sort A better algorithm –separating nodes with indegree 0 –Use a queue to maintain nodes with indegree 0 –O(|E|+|V|)

12 Single-Source Shortest-Path Problem Given a graph, G = (V, E), and a distinguished vertex, s, find the shortest path from s to every other vertex in G. Unweighted shortest paths –Breadth-first search Weighted shortest paths –Dijkstra’s algorithm Assuming no negative edges in graph

13 Unweighted shortest paths (Example) Find shortest paths from v 3 to all other nodes

14 Example (Cont’d) (1) (2) (3) (4)

15 Implementation of unweighted shortest paths Running time O(|V| 2 )

16 A better way Separating unknown nodes with minimum distance Using queue to track the nodes to visit Complexity –O(|E|+|V|)

17 Weighted graphs: Dijkstra’s algorithm The weighted-edge version of the previous algorithm is called Dijkstra’s algorithm for shortest paths The process is equivalent, except that the update of distance function uses the weight of the edge, instead of assuming it 1

18 Example (Source is v 1 )

19 Example (Cont’d)

20 Dijkstra’s algorithm: example 2 A E D CB F

21 Dijkstra’s algorithm: example 2 Step start N A AD ADE ADEB ADEBC ADEBCF D(B),p(B) 2,A D(C),p(C) 5,A 4,D 3,E D(D),p(D) 1,A D(E),p(E) infinity 2,D D(F),p(F) infinity 4,E A E D CB F

22 Implementation of Dijkstra’s Algorithm

23 Implementation (Cont’d) Complexity depends on how smallest distance vertex identified. Sequential search –O(|V| 2 ) Priority queue (heap) –O(|E|log|V|+|V|log|V|) Fibonacci heap –O(|E| + |V|)