CMSC 341 Graphs 2.

Slides:



Advertisements
Similar presentations
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
Advertisements

Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
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.
Chapter 23 Minimum Spanning Trees
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.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
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.
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.
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.
CMSC 380 Graph Traversals and Search. 2 Graph Traversals Graphs can be traversed breadth-first, depth- first, or by path length We need to specifically.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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 & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
CMSC 341 Graphs – DFS Expanded. 2 Depth First Traversal with Finish Times dfs(Graph G) { for (each v  V) d[v] = 0// d = discovery “time” time = 0// “global”
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.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
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)
Introduction to Algorithms
Graphs – Breadth First Search
Graphs Motivation and Terminology Representations Traversals
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
CMSC 341 Graphs.
Introduction to Graphs
Graphs.
CS120 Graphs.
Comp 245 Data Structures Graphs.
CSC 172 DATA STRUCTURES.
Graph Algorithm.
CSE 421: Introduction to Algorithms
CMSC 341 Graphs.
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
BFS,DFS Topological Sort
Graphs Chapter 15 explain graph-based algorithms Graph definitions
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
Shortest Path Algorithms
CMSC 341 Lecture 20.
Shortest Path Algorithms
CSE 421: Introduction to Algorithms
Topological Sort Algorithm
Fundamental Data Structures and Algorithms
Fundamental Structures of Computer Science II
Algorithms Searching in a Graph.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Fundamental Structures of Computer Science II
CSE 417: Algorithms and Computational Complexity
Data structure for graph algorithms: Adjacent list, Adjacent matrix
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 .
Chapter 22: Elementary Graph Algorithms III
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CMSC 341 Graphs 2

Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. Use Dijkstra’s algorithm keep tentative distance for each vertex giving shortest path length using vertices visited so far keep vertex before this vertex (to allow printing of path) at each step choose the vertex with smallest distance among the unvisited vertices (greedy algorithm)

Dijkstra’s Algorithm Vertex v, w; start.dist = 0; for (;;) { v = smallest unknown distance vertex; if (v.path == NOT_A_VERTEX) break; v.known = TRUE; for each w adjacent to v if (!w.known) if (v.dist + cvw < w.dist) { decrease (w.dist to v.dist + cvw); w.path = v; }

Dijkstra Example 3 a b g 1 3 1 2 7 5 3 c f h d 1 6 4 4 2 e j i 1

Edge Types After DFS, edges can be classified into the following types: tree edges -- a discovered vertex v1 encounters an undiscovered vertex v2; the edge between them is a tree edge back edges -- a discovered vertex v1 encounters a discovered but unfinished vertex v2; the edge between them is a back edge. (Graph has a cycle if and only if there is a back edge.) forward edges (directed graphs only) -- a discovered vertex v1 encounters a finished vertex v2 cross edges (directed graphs only) -- a discovered vertex v1 encounters a finished vertex v2 and d[v1] > d[v2]

Edge Types (after DFS completion) Condition Type of Edge (v1, v2)

Traversal Performance What is the performance of DF and BF traversal? Each vertex appears in the stack or queue exactly once. Therefore, the traversals are at least O(|V|). However, at each vertex, we must find the adjacent vertices. Therefore, df- and bf-traversal performance depends on the performance of the getAdjacent operation.

getAdjacent Method 1: Look at every vertex (except u), asking “are you adjacent to u?” List L = new List(<class of vertex>); for (each vertex v, except u) if (isConnected(u,v)) L.doInsert(v); Assuming O(1) performance on isConnected, getAdjacent has O(|V|) performance and traversal performance is O(|V2|);

getAdjacent (cont) Method 2: Look only at the edges which impinge on u. Therefore, at each vertex, the number of vertices to be looked at is D(u), the degree of the vertex (use outdegree for directed graph). This approach is O(D(u)). The traversal performance is since getAdjacent is done O(|V|) times. However, in a disconnected graph, we must still look at every vertex, so the performance is O(max(|V|, |E|)) = O(|V| + |E|). But, what is O(|E|) ??

Number of Edges Theorem: The number of edges in an undirected graph G=(V,E) is O(|V|2) Proof: Suppose G is fully connected. Let p =|V|. We have the following situation: vertex connected to 1 2,3,4,5,…, p 2 1,3,4,5,…, p … p 1,2,3,4,…,p-1 There are p(p-1)/2 = O(|V|2) edges. So O(|E|) = O(|V|2). If df an bf traversal is O(|V|^2) under both approaches to the operation getAdjacent, does it mater which approach is used? Yes. Although the two approaches have the same asymptotic performance, the second approach will be faster for the typical graph.