DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.

Slides:



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

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
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.
CS202 - Fundamental Structures of Computer Science II
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
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.
Graph.
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.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.
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.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
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.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
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.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
Graph Algorithms GAM 376 Robin Burke Fall Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab.
CSC2100B Tutorial 10 Graph Jianye Hao.
Graphs Upon completion you will be able to:
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
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:
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
Graph Algorithm Hongfei Yan School of EECS, Peking University 5/21/2014.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
CS212: Data Structures and Algorithms
CSE 373 Topological Sort Graph Traversals
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
C.Eng 213 Data Structures Graphs Fall Section 3.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CMSC 341 Lecture 21 Graphs (Introduction)
More Graph Algorithms.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Paul Beame in lieu of Richard Anderson
CS223 Advanced Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Chapter 9: Graphs Basic Concepts
Chapter 11 Graphs.
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
CSCI2100 Data Structures Tutorial
Graphs G = (V, E) V are the vertices; E are the edges.
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Graph Algorithms
Graphs G = (V, E) V are the vertices; E are the edges.
CE 221 Data Structures and Algorithms
Chapter 9: Graphs Basic Concepts
GRAPH – Definitions A graph G = (V, E) consists of
Presentation transcript:

DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI

2 ROAD MAP GRAPH ALGORITHMS Definition Representation of Graphs Topological Sort

3 GRAPHS A graph is a tuple G=(V,E) V : set of vertices /nodes E : set of edges each edge is a pair (v,w), where v,w Є V If the pairs are ordered, then the graph is directed directed graphs are sometimes refered as digraphs Vertex w is adjacent to v iff (v,w) Є E

4 GRAPHS A path is a sequence of vertices w 1, w 2, …, w N where (w i, w i+1 ) Є E for 1≤i<N The length of a path is the number of edges on the path, which is equal to N-1 if path contains no edges, length is 0 If the graph contains an edge (v,v) from a vertex to itself, then the path (v,v) is sometimes refered to a loop. A simple path is a path such that all vertices are distinct, except that the first and last could be the same

5 GRAPHS A cycle is a path with length ≥ 1 where w 1 = w N Cycle is simple, if the path is simple In an undirected graph edges should be distinct for simple cycle A directed graph is acyclic if it has no cycles A directed acyclic graph is refered to DAG An undirected graph is connected if there is a path between each pair of vertices A directed graph is strongly connected if there is a path between each pair of vertices A directed graph is weakly connected if the underlying undirected graph is connected A complete graph is a graph in which there is an edge between every pair of vertices

6 GRAPHS Example : Airport system Each airport is a vertex Two vertices are connected by an egde if there is a nonstop flight from the airports that are represented by the vertices The edge could have a weight, representing time, distance or cost of flight. Such a graph is directed It might have longer or cost more to fly in different directions Make sure that the airport system is strongly connected It is always possible to fly from any airport to any other

7 GRAPHS Traffic flow can be modeled by a graph Each street intersection represents a vertex, each street is an edge. Edge costs could represent, among other things, a speed limit or capacity We could ask for the shortest route or use this information to find the most likely location for bottleneck

8 Representation of Graphs We will consider directed graphs We number the vertices, starting at 1. The graph below represents 7 vertices and 12 edges

9 Representation of Graphs Adjacency Matrix Representation Use a two dimensional array to represent a graph For each edge (u,v) Є E we set A [u][v] = 1 A [u][v] = 0, otherwise If the edge has a weight we set A[u][v] = weight we can use -∞ / ∞ to indicate nonexistent edges Space requirement = θ(|v| 2 ) An adjacency matrix is an appropriate representation if the graph is dense: |E| = θ (|v| 2 ) is it true in most applications ?

10 Representation of Graphs Adjacency List Representation If the graph is not dense (is sparse) a better solution is adjacency list representation For each vertex, we keep a list of all adjacent vertices. The space requirement is O(|E|+|V|)

11 Representation of Graphs Adjacency list representation of a graph

12 Topological Sort A topological sort is an ordering of vertices in a directed acyclic graph If there is a path v i to v j, v j appears after v i in the ordering Example : course prerequisite structure

13 Topological Sort course prerequisite example

14 Topological Sort If a graph has a cycle, a topological sort is not possible Ordering is not necessarily unique, any legal ordering will do On the example below, v 1, v 2, v 5, v 4, v 3, v 7, v 6 and v 1, v 2, v 5, v 4, v 7, v 3, v 6 are both topological orderings

15 Topological Sort Algorithm Repeat find a vertex with no incoming edges print the node remove it and its edges Until the graph is empty How to find a vertex with no coming edges ?

16 Topological Sort To find a vertex with no coming edges Linear search on vertices O(|V|)for each find O(|V| 2 ) for topological sort Keep a list (queue or stack) for vertices with zero incoming edges Update the list when an edge is deleted O(|E|)  if adjacency list is used. Total time O(|E|+|V|)

17 /*Simple topological sort pseudocode void Graph::topsort() { Vertex v, w; for (int counter = 0; counter < NUM_VERTICES; counter ++) { v = findNewVertexOfDegreeZero (); if ( v==NOT_A_VERTEX ) throw CycleFound (); v.TopNum = counter ; for each w adjacent to v w.indegree-- ; }

18 Result of Applying topological sort to the graph below

19 /* Pseudocode to perform topological sort void Graph::topsort() { Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()) { v = q.dequeue(); v.topNum = ++counter; // assign next number for each w adjacent to v if (--w.indegree == 0) q.enqueue (w); } if (counter !=NUM_VERTICES) throw CycleFound (); }