Graph Definitions and Applications

Slides:



Advertisements
Similar presentations
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Advertisements

Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Discussion #35 Chapter 7, Section 5.5 1/15 Discussion #35 Dijkstra’s Algorithm.
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Applied Discrete Mathematics Week 12: 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.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
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.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Adjacency Lists; Breadth-First Search & Depth-First Search.
Discussion #30 1/23 Discussion #30 Graph Definitions and Applications.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Data Structures Using C++ 2E
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
Graph Definitions and Applications. Graphs: Very Useful Abstractions Graphs apply to many diverse areas: social sciences, linguistics, physical sciences,
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 Closures of Relations: Transitive Closure and Partitions Sections 8.4 and 8.5.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
All-Pairs Shortest Paths
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
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.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
Graphs Lecture 19 CS2110 – Spring 2013.
Spanning Trees.
Agenda Lecture Content: Introduction to Graph Path and Cycle
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graph & BFS.
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Analysis and design of algorithm
Graphs.
Graph Operations And Representation
Chapter 11 Graphs.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Dynamic Programming.
Chapter 6 Network Flow Models.
Data Structures & Algorithms Digraphs and DAGs
Dynamic Programming.
Networks Prim’s Algorithm
Graphs.
GRAPHS Lecture 17 CS2110 Spring 2018.
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Parallel Graph Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Chapter 9 Graph algorithms
GRAPHS.
Presentation transcript:

Graph Definitions and Applications

Graphs: Very Useful Abstractions Graphs apply to many diverse areas: social sciences, linguistics, physical sciences, communication engineering, and many more. Why? Because they are a useful abstraction. Abstractions capture the essence of a real-world problem. Abstractions discard irrelevant details and let us focus on the essence of the problem to be solved. Abstractions lend themselves to precise (mathematical) specification. Mathematically defined abstractions lead to correct computer implementations.

Graphs in Programming Testing: paths in programs: Call graphs in programs Software products consist of modules that invoke each other Deadlock in operating systems T F T F T F … Sequence If-then-else While Repeat-until Case

Graphs in Transportation & Product Delivery Airline connections: getting from here to there Highways with mileage between cities Traveling salesman problem: visit all cities with minimal distance Traffic networks studied by transportation and city planners Pipelines for delivering water or gas

Graphs in Networks Social networks Computer networks Friend of a friend Co-collaborators Computer networks Connect all, minimal number of connections (Minimal) spanning trees, LANs, WANs Scheduling networks Workflow from source to sink Critical path, slack time Earliest starting time, latest completion time

Problems that Don’t Initially Look Like Graph Problems Have you ever had to work your way through a maze of interpreters at an international conference? Swedish and Indonesian delegates wish to talk to each other, but among the sixteen interpreters, not one speaks both Swedish and Indonesian. A way to solve this problem is to form a chain of interpreters. There are, however, only four booths; interpreters must be in booths, and each booth can contain only one interpreter. Problem: Does a solution exist? If so, which four interpreters will you place in the booths?

Interpreters at the Great World Conference Swedish—( Booth #1 Booth #2 Booth #3 Booth #4 )—Indonesian (1) Portuguese Indonesian (2) Polish English German (3) Italian Norwegian (4) Korean Turkish (5) (6) Swedish (7) Spanish Chinese Japanese (8) French (9) Dutch (10) (11) Russian (12) (13) (14) (15) (16)

Table of Spoken Languages 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B Chinese Dutch English French German Indonesian Italian Japanese Korean Norwegian Polish Portuguese Russian Spanish Swedish Turkish

“Can Speak to” Matrix A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B

Graph of “Can Speak to” Matrix 5 Polish Polish 11 English Polish English 13 2 Indonesian Swedish German Swedish English English Indonesian Indonesian 16 Swedish Italian Russian Japanese German English Norwegian French Indonesian A 10 3 15 B Swedish Dutch German Dutch 14 9 Indonesian Indonesian French Swedish Swedish Dutch Chinese 1 Portuguese 6 12 Spanish Korean Portuguese Turkish Chinese French Chinese Portuguese 4 8 7

Graph Definition Hyp: edges either all ordered or all unordered G = (V, E), where V is a set of vertices/nodes, and E is a relation on VxV (E is a set of all-ordered or all-unordered pairs) Example 1 2 3 V = {1, 2, 3} E = {(1, 2), (2, 3), (3, 3)}

A Few Special Graphs H is a subgraph of G if VH is a subset of VG and EH is a subset of EG A graph G is complete if all nodes in G are connected to all other nodes in G (often denoted K1, …, Kn) A graph G is planar if it can be drawn (in 2D) with no crossing lines  K5 B3 d = directed u = undirected A graph is planar if and only if it does not contain either K5 or B3 as a subgraph.

Adjacency Matrix Definition: Let G = (V, E) be a simple digraph. Let V = {v1, v2,…vn} be the vertices (nodes). Order the vertices from v1 to vn. The n  n matrix A whose elements are given by aij = 1, if (vi, vj)  E 0, otherwise is the adjacency matrix of the graph G. Example: { 1 2 3 4 1 v4 v3 v2 v1 A =

Adjacency Lists For directed graphs: b c a a 2 3 b c Simple Notation b 1 a 2 3 b c b c a Simple Notation b a c a b c a 1 1 1 { (a,a), (a,b), (a,c), (b,c), (c,b) } b 1 c 1

Adjacency Lists for Undirected and Weighted Graphs Undirected Graphs: b a c 5 3 7 1 a b c Make each edge (except loops) go both ways. Weighted Graphs: a (a,1) (b,5) (c,3) b (a,5) (c,7) c (a,3) (b,7) add additional field to node node-weight pairs

Space: Adjacency Lists vs. Matrices Space (n vertices and m edges) matrix: n2 + n(vertex-name size) = matrix size + header size matrix can be bits, but bits are not directly addressable list: n(header-node size) + m(list-node size) Sparse: few edges  0 in the extreme case Matrix  fixed size: so no size benefit List  variable size: as little as n(vertex-node size) Dense: many edges  n2 in the extreme case Matrix  fixed size: so no size loss List  variable size: as much as n(header-node size) + n2(list-node size)

Operations: Adjacency Lists vs. Matrices Operations depend on sparse/dense and what’s being done. Examples (n nodes and m edges) Is there an arc from x to y? Matrix: O(1)  check value at (x, y) List: O(n)  index to x, traverse list to y or end Get successor nodes of a node. Matrix: O(n)  scan a row List: O(n)  traverse a linked list Get predecessor nodes of a node. Matrix: O(n)  scan a column List: O(n+m)  traverse all linked lists, which could be as bad as O(n+n2) = O(n2).

Powers of Adjacency Matrices Powers of adjacency matrices: A2, A3, … Can compute powers Using ordinary arithmetic: Using Boolean arithmetic: aij =  k=1 n aikakj aij =  k=1 n aikakj

Powers Using Ordinary Arithmetic (I) 1 2 3 4 1 2 3 4 1 1 A = 2 1 1 3 1 4 1 1 2 1 4 3 A2 = <1,2,4> <2,3,4> <2,4,4> <3,4,4> <4,2,4> <4,4,4> 4 1 2 3 A3 = <1,2,3,4> <1,2,4,4> <2,4,2,4> <2,4,4,4> <2,3,4,4> <3,4,2,4> <3,4,4,4> <4,2,3,4> <4,2,4,4> <4,4,2,4> <4,4,4,4>

Powers Using Ordinary Arithmetic (II) 1 2 3 4 7 2 4 1 3 6 A4 = <1,2,3,4,4> <1,2,4,2,4> <1,2,4,4,4> <2,4,2,4,4> <2,4,4,4,4> <2,3,4,2,4> <2,3,4,4,4> <2,4,2,3,4> <2,4,4,2,4> <3,4,4,4,4> <3,4,2,4,4> <3,4,4,2,4> <3,4,2,3,4> <4,4,4,4,4> <4,4,4,2,4> <4,4,2,4,4> <4,2,4,4,4> <4,2,4,2,4> <4,2,3,4,4> <4,4,2,3,4> The element in the ith row and the jth column of An is equal to the number of paths of length n from the ith node to the jth node.

Powers Using Ordinary Arithmetic (III) 1 4 3 2 A = A1 + A2 = # of ways in 2 or fewer A1 + A2 + A3 = # of ways in 3 or fewer A1 + A2 + A3 + A4 = # of ways in 4 or fewer 2 1 4 3 A2 = 7 2 4 1 3 6 0 + 1 + 2 = WHY????? 4 1 2 3 A3 = 1 + 2 + 4 =

Powers Using Boolean Arithmetic Using Boolean arithmetic, we compute reachability. We can compute the paths of length 2 by A2, 3 by A3… and of length n by An. Since the longest possible path we care about for reachability is n, we obtain all paths by A  A2  A3  … An. The reachability matrix is the transitive closure of A. Algorithm: for (j = 1 to n) { for (k = 1 to n) { aijk = 0; for (m = 1 to n) { aijk = aijk  (ai-1jm  a1mk); } R = A; for (i = 2 to n) { compute Ai; R = R  Ai; } O(n4) (n-1)nnn

Clearly more efficient than our straightforward reachability algorithm Warshall’s Algorithm A more efficient way to compute reachability. Algorithm: 1. Initialize adjacency matrix A: (For an edge from i to j, aij is set to 1; otherwise, 0.) 2. for (k=1 to n) for (i=1 to n) for (j=1 to n) if (aij = 0) aij  aik  akj Clearly more efficient than our straightforward reachability algorithm O(n3) < O(n4); but does it work?

Intuitive Idea We’ll see that after k outer loops, aij = 1 iff there is a path from i to j directly or through {1, 2, …, k}. 3 4 1 5 6 2 for (k=1 to n) for (i=1 to n) for (j=1 to n) if (aij = 0) aij  aik  akj k is called a pivot k=1: a41  a13 adds (4,3) k=2: a32  a26 adds (3,6), a62  a26 adds (6,6) 1 2 3 4 5 6 1 1 1 k=3: a13  a32 adds (1,2), a43  a32 adds (4,2), a43  a36 adds (4,6), a13  a36 adds (1,6) 1 1 1 1 1 1 1 1 k=4 and k=5 add nothing (either no in or no out) k=6: adds (2,5), (3,5), (1,5), (4,5), (2,2) 1

Floyd’s Algorithms

Shortest Distance in a Weighted Graph 5 2 3 4 1 6 8 Minimum Dist[1,4] = 6 Minimum Dist[3,2] = 

Variation on Warshall’s Algorithm: Pivot on node k Floyd’s Algorithm Variation on Warshall’s Algorithm: Pivot on node k Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 O(n3) 1 2 3 4 5  8 6 Point out that initial matrix is the edge weights or infinity. Initial Dist[i,j] = The initial matrix has the edge weights or infinity

Floyd’s Algorithm (continued…) Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 Pivot k: k=1: nothing 1 2 3 4 5  8 6 1 2 3 4 5 8 7  6 k=2: 13:3, 15:7 k=1 k=2

Floyd’s Algorithm (continued…) Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 Pivot k: k=1: nothing 1 2 3 4 5 8 7  6 1 2 3 4 5 6  k=2: 13:3, 15:7 k=3: 14:13+34:6 15:4 24:5 25:3 45:5 44:0 (not changed) k=2 k=3

Floyd’s Algorithm (continued…) Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 Pivot k: k=1: nothing 1 2 3 4 5 6  1 2 3 4 5 6  k=2: 13:3, 15:7 k=3: 14:13+34:6 15:4 24:5 25:3 45:5 44:0 (not changed) k=4: no changes k=5: nothing k=3 k=4

Notes on Warshall’s and Floyd’s Algorithms Warshall’s and Floyd’s algorithms do not produce paths. Can keep track of paths Update as the algorithms execute Warshall’s and Floyd’s algorithms only work for directed graphs. For undirected graphs, replace each edge with two directed edges Then, run algorithms as given

Dijkstra’s Algorithm

Dijkstra’s Algorithm Find the shortest weighted path between two given nodes (Easier to find the minimum from one to all) Algorithm b c a e d 5 2 1 10 Initialize D (distance) table Until all nodes are settled, 2a. find smallest distance & settle node 2b. adjust distances in D for unsettled nodes

Trace of Dijkstra’s Algorithm b c a e d 5 2 1 10 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node -adjust distances in D for unsettled nodes e d c b a settled distance iteration a  1 2 5 1 a,d  2 5 adjust w/d 2 a,d,c 2 5 adjust w/c 4 a,d,c,e 2 4 3 adjust w/e Distances from a if two the same, choose the first one All circled nodes are shortest distances from node a to node x Settled nodes are the shortest distances Can I derive a shortest path? a,d,c,e,b 4 all nodes settled Circled numbers: shortest path lengths from a.

Trace with Different Weights b c a e d 6 2 4 1 10 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node -adjust distances in D for unsettled nodes e d c b a settled distance iteration a  1 4 6 1 a,d  4 6 adjust w/d 2 a,d,e 2 4 6 adjust w/e 3 a,d,e,c 3 6 adjust w/c Distances from a if two the same, choose the first one All circled nodes are shortest distances from node a to node x Settled nodes are the shortest distances Can I derive a shortest path? 5 a,d,e,c,b 5 4 all nodes settled Circled numbers: shortest path lengths from a.

Practice Exercise

Big-Oh for Dijkstra’s Algorithm Adjacency List: b c a e d 5 2 1 10 D(x) a (b,5) (c,2) (d,1) 5 b (a,5) (c,2) 2 c (a,2) (b,2) (d,10) (e,1) 1 d (a,1) (c,10) (e,1)  e (d,1) (c,1)  Unroll the loop: n + k1 + n + k2 + … + n + k(n-1) = (n1)n = O(n2) = 2(m  # of edges in the initialization) = O(m) 1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. O(n)  visit at most all edges for a node  can’t be more than n O(n)  each node needs to be settled 2a. O(n)  look through D(x) list to find smallest 2b. O(k)  for chosen node, go through list of length k { O(n2) dominates Over all iterations, the k’s add up to 2(m  # of edges in the initialization).

Dijkstra’s Algorithm vs. Floyd’s Dijkstra’s algorithm is O(mlogn) = O(n2logn) in the worst case Floyd’s algorithm is O(n3), but computes all shortest paths Dijkstra’s algorithm can compute all shortest paths by starting it n times, once for each node. Thus, to compute all paths, Dijkstra’s algorithm is O(nmlogn) = O(n3logn) in the worst case Which is better depends on the application Dijkstra’s algorithm is better if only a path from a single node to another or to all others is needed For all paths, Dijkstra’s algorithm is better for sparse graphs, and Floyd’s algorithm is better for dense graphs