Basic Graph Algorithms Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Chapter 8, Part I Graph Algorithms.
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.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
Maximum Flow Computation Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.
1 Representing Graphs. 2 Adjacency Matrix Suppose we have a graph G with n nodes. The adjacency matrix is the n x n matrix A=[a ij ] with: a ij = 1 if.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Graph & BFS.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
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.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
CS8803-NS Network Science Fall 2013
Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng
More Dynamic Programming Floyd-Warshall Algorithm.
CSCI-455/552 Introduction to High Performance Computing Lecture 18.
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.
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 9, 2014.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
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.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Theory. undirected graph node: a, b, c, d, e, f edge: (a, b), (a, c), (b, c), (b, e), (c, d), (c, f), (d, e), (d, f), (e, f) subgraph.
The Graph Data Structure Mugurel Ionu Andreica Spring 2012.
Design and Analysis of Algorithms Introduction to graphs, representations of a graph Haidong Xue Summer 2012, at GSU.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graphs and Shortest Paths Using ADTs and generic programming.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009
Graphs Representation, BFS, DFS
Lecture 11 Graph Algorithms
Graphs.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Network Science: A Short Introduction i3 Workshop
Enumerating Distances Using Spanners of Bounded Degree
Graphs Representation, BFS, DFS
Lecture 7 All-Pairs Shortest Paths
Graphs CSE 2011 Winter November 2018.
Analysis and design of algorithm
3.5 Minimum Cuts in Undirected Graphs
Shortest Path Algorithms
ITEC 2620M Introduction to Data Structures
Chapter 24: Single-Source Shortest Paths
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 24: Single-Source Shortest Paths
Lecture 10 Graph Algorithms
Chapter 9 Graph algorithms
Presentation transcript:

Basic Graph Algorithms Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009

Outline Introduction/review of graphs Some basic graph problems & algorithms Start of an example question from ICPC’07 (“Tunnels”)Tunnels

Relation to Contests Many programming contest problems can be viewed as graph problems. Some graph algorithms are complicated, but a few are very simple. If you can find a way to apply one of these, you will do well.

How short & simple? int [][] path = new int[edge.length][edge.length]; for (int i =0; i < n; i++) for (int j = 0; j < n; j++) path[i][j] = edge[i][j]; for (int k = 0; k < n; k++) for (int i =0; i < n; i++) for (int j = 0; j < n; j++) if (path[i][k] != 0 && path[k,j] != 0) { x = path[i][k] + path[k][j]; if ((path[i,j] == 0) || path[i][j] > x) path[i][j] = x; }

Directed Graphs G = (V, E) V = set of vertices (a.k.a. nodes) E = set of edges (ordered pairs of nodes)

Directed Graph V = { a, b, c, d } E = { (a, b), (c, d), (a, c), (b, d), (b, c) } c b d a

Undirected Graph V = { a, b, c, d } E = { {a, b}, {c, d}, {a, c}, {b, d}, {b, c} } c b d a

Undirected Graph as Directed V = { a, b, c, d } E = { (a, b), (b,a),(c,d),(d,c),(a,c),(c,a), (b,d),(d,b),(b,c)(c,b)} c b d a Can also be viewed as symmetric directed graph, replacing each undirected edge by a pair of directed edges.

Computer Representations Edge list Hash table of edges Adjacency list Adjacency matrix

Edge List Often corresponds to the input format for contest problems Container (set) of edges may be used by algorithms that add/delete edges.

Adjacency List Can save space and time if graph is sparse with pointers & dynamic allocation: with two arrays:

Hash Table (Associative Map) good for storing information about nodes or edges, e.g., edge weight H(1,2) 1 H(0,1) 1 etc.

Adjacency/Incidence Matrix A[i][j] = 1 → (i,j) i  E A[i][j] = 0 otherwise a very convenient representation for simple coding of algorithms, although it may waste time & space if the graph is sparse.

Some Basic Graph Problems Connectivity, shortest/longest path –Single source –All pairs: Floyd-Warshall AlgorithmFloyd-Warshall Algorithm dynamic programming, efficient, very simple MaxFlow (MinCut) Iterative flow-pushing algorithms

Floyd-Warshall Algorithm Assume edgeCost(i,j) returns the cost of the edge from i to j (infinity if there is none), n is the number of vertices, and edgeCost(i,i) = 0 int path[][]; // a 2-D matrix. // At each step, path[i][j] is the (cost of the) shortest path // from i to j using intermediate vertices (1..k-1). // Each path[i][j] is initialized to edgeCost (i,j) // or ∞ if there is no edge between i and j. procedure FloydWarshall () for k in 1..n for each pair (i,j) in {1,..,n}x{1,..,n} path[i][j] = min ( path[i][j], path[i][k]+path[k][j] ); * Time complexity: O (|V| 3 ).

Details Need some value to represent pairs of nodes that are not connected. If you are using floating point, there is a value ∞ for which arithmetic works correctly. But for most graph problems you may want to use integer arithmetic. Choosing a good value may simplify code When and why to use F.P. vs. integers is an interesting side discussion.

if (path[i][k] != 0 && path[k,j] != 0) { x = path[i][k] + path[k][j]; if ((path[i,j] == 0) || path[i][j] > x) path[i][j] = x; } Suppose we use path[i][j] == 0 to indicate lack of connection. Example

ij k path[i][j] path[i][k] path[k,j] paths that go though only nodes 0..k-1 How it works

Correction In class, I claimed that this algorithm could be adapted to find length of longest cycle-free path, and to count cycle-free paths. That is not true. However there is a generalization to find the maximum flow between points, and the maximum-flow path: for k in 1,..,n for each pair (i,j) in {1,..,n}x{1,..,n} maxflow[i][j] = max (maxflow[i][j] min (maxflow[i][k], maxflow[k][j]);