Chapter 9 – Graphs A graph G=(V,E) – vertices and edges

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Chapter 23 Minimum Spanning Trees
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Data Structures Using C++ 2E
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
CS 146: Data Structures and Algorithms July 21 Class Meeting
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.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
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:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
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.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Graphs Representation, BFS, DFS
Minimum Spanning Trees
C.Eng 213 Data Structures Graphs Fall Section 3.
Introduction to Graphs
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph Algorithm.
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Graphs.
Chapter 11 Graphs.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Chapter 9 – Graphs A graph G=(V,E) – vertices and edges V is a set of vertices – nodes E is a set of edges defined as vertex pairs (u,v) where u and v are elements of V Vertex w is adjacent to vertex z if there exists an edge (w,z) in E If edge (a,b) indicates we can only go from a to b, then we have a directed graph If edge (a,b) indicates we can go either way, then we have an undirected graph. Sometimes an edge has a weight

Applications We can use graphs to represent Computer networks Nodes are sites – edges are connections Weight may be transit time Highway system Nodes are cities – edges are roads Weights could be time or distance Airline system Nodes are cities – edges are plane routes Weights could be cost, distance, or time

More Definitions A path is a sequence of vertices w1, w2, …, wn such that (wi,wi+1) are edges in E The length of a path is the number of edges (n-1) A cycle in a graph is a path of length at least 1 such that w1=wn A simple path has all vertices distinct A graph is acyclic if it has no cycles DAG – directed acyclic graph

Representing Graphs We draw a graph with circles and connecting lines Can use an adjacency matrix representation If there are n vertices, then we create an n x n matrix The matrix will have a 1 in position (i,j) if there is an edge from vertex i to vertex j. The matrix has a 0 in position (i,j) if there is no edge between vertex i and vertex j. An undirected graph will be symmetric

Weighted Graph Instead of using a 1 for a connection, put in the weight of that edge. For some algorithms you could use +infinity or – infinity for no connection depending on the algorithm Space requirements are |V|2 This is fine if the matrix is dense (i.e. there are many edges compared to |V|2)

Another Representation If there are not many edges can use the adjacency list representation Keep an array of head pointers of size |V| Each element points to a linked list of vertices that are adjacent to the current vertex The space requirements for this representation is |V|+|E| If the edges have weights, we can store that information in the nodes of the linked list Undirected graphs use 2x the storage

Topological Sort An ordering of vertices in a DAG such that if there is a path from vi to vj, then vj appears after vi in the ordering. This is useful in determining the order to take courses if the DAG represents courses and their prerequisites Cannot be done if there is a cycle The ordering is not necessarily unique

Topological Sort Algorithm Find a vertex with no incoming edges Print that vertex out Remove that vertex from the graph and all the edges that go out of it Repeat this process until there are no more vertices left Will be useful to keep an indegree with each vertex Update the indegree when you remove the node

Analysis of Topological Sort Find a vertex with no incoming edges takes O(|V|) Will do this |V| times Entire algorithm is O(|V|2) Can make the find vertex more efficient Keep a list of all vertices with indegree 0 Find is now O(1) When remove vertex and update indegrees, add to the list. Now algorithm is O(|V|+|E|)

Shortest Path Algorithms Single source, shortest weighted path problem Single source, shortest unweighted path problem Shortest path to one vertex or all vertices Assume no path has negative cost Currently there is no algorithm to find the shortest path to a specific node that is any faster than finding the shortest path to all nodes

Unweighted Shortest Paths Find the shortest path from vertex s to all other vertices Look at all vertices adjacent to s Enter the value 1 in entries associated with those vertices Look at all the vertices with an entry of 1 and find which vertices are adjacent to them. Enter a 2 into those entries (that do not have a value entered). Repeat until all vertices accounted for.

Analysis of Algorithm The longest path will be at most |V|. Will repeat the loop at most |V| times. The search for vertices with entry 1 is O(|V|). Algorithm is O(|V|2). Can be made more efficient by keeping a list for vertices with path length k and path length k+1. As you go through the list of vertices with path length k, create list of vertices with path length k+1 to be used in the next loop. Now O(|V|+|E|).

Dijkstra’s Algorithm Now what if the graph has weighted edges? Use the same basic algorithm as with the unweighted graph. Instead of keeping a value of the path length with each vertex, we keep a tentative path length. Start the same way. Start at the starting node and see which nodes are adjacent to it.

Shortest Weighted Path Uses a greedy algorithm. Greedy algorithms do not always work This one does not work if a path weight is negative. Keep a queue of vertices that can be processed (vertices adjacent to those that have been processed) and the weight to that vertex. Check to see if getting to that vertex is better than any other way to that vertex yet. Update the tentative path length if better. Add to queue if better. Select the vertex with the smallest weight.

Example from the book Done on board

Analysis Using trivial find the minimum would take O(|V|) time to find the minimum. Do this |V| times and we get a O(|V|2) algorithm. Really this is O(|E|+|V|2), but since |E|<=O(|V|2) we get the above result. If |E| is much less than |V|2 then we could keep the list in a priority queue. Now the algorithm is O(|E| log |V| + |V| log |V|) which is O(|E| log |V|). The |E| log |V| term is from the insert.

Minimum Spanning Tree Find a tree formed from graph edges that connects all the vertices of G with lowest cost. Can perform this on directed or undirected; weighted or unweighted graphs. Result will be a tree since it will have no cycles. We will look at weighed undirected graphs.

Prim’s Algorithm Pick any starting vertex. Place it in the tree. Find all edges adjacent to all vertices in the tree. Pick the one with the smallest weight. Do not pick an edge what would cause a cycle (the ending vertex is already in the tree). Add that edge/vertex to the graph. Repeat until all vertices are processed (if graph is connected).

Kruskal’s Algorithm Sort edges by weights. Pick the next edge to add to the tree as the smallest edge not already picked that will not cause a cycle (the endpoint vertices are not both in the tree).

Depth First Search Start at some vertex Visit an adjacent vertex and search all of its adjacent vertices before returning to the original vertex to search the rest of its adjacent vertices. Must worry about cycles. Easily implement as a preorder recursive general traversal with a marker for already visited (to prevent cycles).