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.

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.
Part A - Terminology and Traversals
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
§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.
Algorithms and Data Structures
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Graph Algorithms: Part 1
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 / 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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Social Media Mining Graph Essentials.
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
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
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.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
COSC 2007 Data Structures II Chapter 14 Graphs III.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
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.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
 Definition of Graphs:  Graph Representations  Graph Traversal  DFS (Topological Sort), BFS (Fewest Hops)  Sub-graphs, Embedded Trees  Minimum Spanning.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
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.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
CMSC 341 Graphs.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Chapter 11 Graphs.
CMSC 341 Lecture 20.
Shortest Path Algorithms
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Graph Algorithms
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
Elementary Graph Algorithms
GRAPH – Definitions A graph G = (V, E) consists of
CMSC 341 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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

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 the edges are ordered the graph is called directed. Sometimes called a Digraph. Vertex v is adjacent to vertex u iff (u,v)  E. A weighted edge has a cost or weight associated with it.

A path is a sequence of vertices v 1, v 2,... v n such that (v i,v i+1 )  E for 1  i  n The length of the path is the number of edges, which is n-1 Some special cases: path of length 0, and a loop. A simple path is one in which each vertex is distinct, except possibly the first and last A cycle in a directed graph is a path of length at least 1 such that v 1 = v n.

A directed acyclic graph (DAG) is a directed graph containing no cycles. Connected graph: undirected graph where each node is reachable from every other node. Strongly connected: A directed graph which is connected. Weakly connected: a directed graph where the underlying undirected graph is connected.

A graph is completely connected if there is an edge from every vertex to every other vertex. Graph S = ( V ’, E ’ ) is a subgraph of graph G = (V, E) iff V ’  V and E ’  E Indegree and Outdegree of a vertex is the number of edges coming in and going out respectively A graph is dense if |E| =  (|V| 2 ). Otherwise, a graph is sparse.

Representing Graphs Pictorially using dots for vertices and lines for edges. Adjacency Matrix for dense graphs: use a |V| by |V| matrix A where A [u][w] = 1 if (u,w)  E and 0 otherwise. Also nonzero entries can represent weights. Adjacency List for sparse graphs.

Graph Traversals A tree is a graph (DAG) Just as we can traverse a tree in a depth-first or a breadth-first manner, so we can traverse a graph in a breadth-first or depth- first manner.

Depth-First Traversal Algorithm DFS( Graph G, Vertex V ) Visit( V ) Mark( V ) For each vertex W adjacent to V If W is not marked DFS( G, W )

Breadth-First Traversal Algorithm BFT( Graph G, Vertex S ) Mark(S); Visit (S); Q.Enqueue(S) While the Queue is not empty V = Q.Dequeue() For each vertex W adjacent to V that is not marked Mark( W ) Visit( W ) Q.Enqueue( W )

Topological Sort A topological sort of a directed acyclic graph is an ordering of the vertices such that vertex w appears after vertex v in the ordering if there is a path from v to w. A simple algorithm is to find each vertex of indegree 0, print them, remove the vertices and all their edges from the graph, and repeat the procedure. This algorithm can be implemented by a depth-first traversal.

A better way is to use a queue. –First put all vertices with indegree 0 in the queue. –While the queue is not empty, dequeue a node, print it out, and decrement the indegrees of all its adjacent nodes. –If the indegree of a vertex becomes 0, put it into the queue. –If the queue becomes empty before all nodes are printed, the graph contains a cycle.

Single-Source Shortest Paths Unweighted Version: Given as input an unweighted graph G = (V, E), and a distinguished vertex s, find the shortest path from s to every other vertex in G. In this version, we traverse the graph in a breadth-first manner, and assume initially that each node has distance  from s.

Pseudocode for shortest path (unweighted) First enqueue s While the queue is not empty –remove a vertex v from the queue –for each vertex w that is adjacent to v if w’s distance from s is infinity –set the distance to 1 + v’s distance –enqueue w

Dijkstra’s Algorithm This algorithm is for the single-source shortest path problem in a weighted graph having no negative cost edges. This is an example of a greedy algorithm. –At each stage, the vertex v having the shortest distance from s is selected from the unknown vertices. Then, the path from s to v is declared known.

Dijkstra’s Algorithm (Edsger Dijkstra) Input: D[], Graph G, Vertex S D[S] = 0 while there are unmarked vertices V=least distance unmarked vertex if D[V] =  STOP: V is not reachable mark V for each vertex W adjacent to V if W is not marked if D[V] + d(V,W) < D[W] D[W] = D[V] + d(V,W)

Negative Costs Bellman-Ford Dijkstra’s algorithm does not work if there are negative cost edges. The Bellman-Ford algorithm uses a Queue (Q) and keeps track of the vertices already in queue. Again, the initial node is S.

Q.enqueue( S ) while Q is not empty V = Q.dequeue() For each vertex W adjacent to V if D[V] + d(V,W) < D[W] D[W] = D[V] + d(V,W) if W is not in Q, Q.enqueue(W)

Negative Cost Cycles The preceding works if there are negative cost edges, but not if there are negative cost cycles For negative cost cycles, we must keep track of how many times a vertex is put into the queue. The algorithm terminates if any vertex is dequeued |V| + 1 times

All-Pairs Shortest Path Find the shortest distance between all pairs of vertices in the graph. This is the Floyd- Warshall algorithm. We may assume negative-cost edges, but not negative-cost cycles. This is a dynamic-programming solution that runs in O( |V| 3 ) time.

Floyd-Warshall I Let G( V, E ) be a graph of N nodes W[N+1][N+1] is the weight matrix, where for two nodes, i and j: W[i][j] = 0, if i = j W[i][j] = w i,j if (i,j) is an edge W[i][j] =  if i  j and (i,j) is not an edge

Floyd-Warshall II Let P[N+1][N+1] be a predecessor matrix, where for two nodes, i and j: P[i][j] = NOT_A_VERTEX, if i = j or if W[i][j] =  P[i][j] = i, if W[i][j] < 

Floyd-Warshall III for k = 1 to N for i = 1 to N for j = 1 to N if( W[i][j] > W[i][k] + W[k][j] ) W[i][j] = W[i][k] + W[k][j] P[i][j] = P[k][j]

Minimum Cost Spanning Tree Given a undirected graph G, a MCST is a tree that connects each node of graph with a total path length that is a minimum. There are two basic algorithms, both of which are greedy algorithms Prim’s Algorithm Kruskal’s Algorithm

Prim’s Algorithm Grow the tree one vertex at a time. At any given time, the graph is divided into two sets of vertices: those in the tree (A) and those not (B). At each stage, add the least cost edge that connects a vertex in set A to a vertex in set B. Then add this vertex to set A.

Kruskal’s Algorithm Greedy Strategy Select edges in order of smallest cost, adding an edge if it does not cause a cycle Initially, there is a forest of |V| single-node trees. Each stage adds an edge: merges two trees. When there is a single tree, the algorithm finishes.

Directed Example

Undirected Example