Weighted Graphs. Computing 2 COMP x1. Sedgewick Part 5: Chapter

Similar documents
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Cpt S 223. School of EECS, WSU

Social Media Mining. Graph Essentials

Minimum Spanning Trees

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

CIS 700: algorithms for Big Data

Exam study sheet for CS2711. List of topics

Data Structure [Question Bank]

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures and Algorithms Written Examination

Any two nodes which are connected by an edge in a graph are called adjacent node.

Bicolored Shortest Paths in Graphs with Applications to Network Overlay Design

Analysis of Algorithms, I

Computational Approach for Assessment of Critical Infrastructure in Network Systems

International Journal of Software and Web Sciences (IJSWS)

Chapter 6: Graph Theory

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Part 2: Community Detection

WAN Wide Area Networks. Packet Switch Operation. Packet Switches. COMP476 Networked Computer Systems. WANs are made of store and forward switches.

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Load balancing Static Load Balancing

A number of tasks executing serially or in parallel. Distribute tasks on processors so that minimal execution time is achieved. Optimal distribution

MATHEMATICS Unit Decision 1

B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 G ABG 6 A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 G BG 1

6 March Array Implementation of Binary Trees

Data Structures. Chapter 8

Sample Questions Csci 1112 A. Bellaachia

Finding and counting given length cycles

Algorithms and Data Structures

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

An Introduction to APGL

Load Balancing and Termination Detection

CS711008Z Algorithm Design and Analysis

Graph/Network Visualization

Analyzing the Facebook graph?

A Review And Evaluations Of Shortest Path Algorithms

Binary Heap Algorithms

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Approximation Algorithms

Graph Mining and Social Network Analysis

The number of marks is given in brackets [ ] at the end of each question or part question. The total number of marks for this paper is 72.

Load Balancing and Termination Detection

Network (Tree) Topology Inference Based on Prüfer Sequence

Mining Social Network Graphs

International Journal of Advanced Research in Computer Science and Software Engineering

Asynchronous Computations

OPTIMAL DESIGN OF DISTRIBUTED SENSOR NETWORKS FOR FIELD RECONSTRUCTION

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Algorithm Design and Analysis

Scheduling Shop Scheduling. Tim Nieberg

Data Structures Fibonacci Heaps, Amortized Analysis

Load Balancing and Termination Detection

Graph Theory Algorithms for Mobile Ad Hoc Networks

Binary Heaps. CSE 373 Data Structures

Distance Degree Sequences for Network Analysis

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Warshall s Algorithm: Transitive Closure

Solutions to Homework 6

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Converting a Number from Decimal to Binary

DATA STRUCTURES NOTES FOR THE FINAL EXAM SUMMER 2002 Michael Knopf

Systems and Algorithms for Big Data Analytics

Cpt S 223. School of EECS, WSU

The Goldberg Rao Algorithm for the Maximum Flow Problem

Big O and Limits Abstract Data Types Data Structure Grand Tour.

CS311H. Prof: Peter Stone. Department of Computer Science The University of Texas at Austin

Now is the time. For all good men PERMUTATION GENERATION. Princeton University. Robert Sedgewick METHODS

Seminar. Path planning using Voronoi diagrams and B-Splines. Stefano Martina

Graph Theory and Topology Design

Chapter 7 Load Balancing and Termination Detection

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Simplified External memory Algorithms for Planar DAGs. July 2004

SCAN: A Structural Clustering Algorithm for Networks

Social Media Mining. Network Measures

CompSci-61B, Data Structures Final Exam

Problem Set 7 Solutions

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

NP-Completeness. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Fast Edge Splitting and Edmonds Arborescence Construction for Unweighted Graphs

Distributed Computing over Communication Networks: Maximal Independent Set

Static Load Balancing

MATHEMATICAL THOUGHT AND PRACTICE. Chapter 7: The Mathematics of Networks The Cost of Being Connected

PES Institute of Technology-BSC QUESTION BANK

2.8 An application of Dynamic Programming to machine renewal

DATA ANALYSIS II. Matrix Algorithms

PROTOTYPE TO DESIGN A LEASED LINE TELEPHONE NETWORK CONNECTING LOCATIONS TO MINIMIZE THE INSTALLATION COST

Probe Station Placement for Robust Monitoring of Networks

Protein Protein Interaction Networks

CSE 4351/5351 Notes 7: Task Scheduling & Load Balancing

Graphs without proper subgraphs of minimum degree 3 and short cycles

SEMITOTAL AND TOTAL BLOCK-CUTVERTEX GRAPH

ONLINE DEGREE-BOUNDED STEINER NETWORK DESIGN. Sina Dehghani Saeed Seddighin Ali Shafahi Fall 2015

A Fast Algorithm For Finding Hamilton Cycles

CSE331: Introduction to Networks and Security. Lecture 8 Fall 2006

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Transcription:

Weighted Graphs Computing 2 COMP1927 17x1 Sedgewick Part 5: Chapter 20.1-20.4 21.1-21.3

WEIGHTED GRAPHS Some applications require us to consider a cost or weight costs/weights are assigned to edges Often use a geometric interpretation of weights low weight - short edge high weight - long edge Weights are not always geometric Some weights can be negative this can make some problems more difficult! Assume in our graphs we have non-negative weights

EXAMPLE: WEIGHTED GRAPHS Example: map of airline flight routes vertices = airports edge = flights weights = distance/time/price

WEIGHTED GRAPH IMPLEMENTATION Adjacency Matrix Representation change 0 and 1 to float/double Need a special float constant to indicate NO_EDGE Can t use 0. It may be a valid weight Adjacency Lists Representation add float weight to each node This will work for directed or undirected graphs

ADJACENCY MATRIX WITH WEIGHTS

ADJACENCY LIST REPRESENTATION WITH WEIGHTS

WEIGHTED GRAPH PROBLEMS Minimal spanning tree find the minimal weight set of edges that connect all vertices in a weighted graph might be more than one minimal solution we will assume undirected graph we will assume non-negative weights e.g., lowest cost to build an electrical network of circuits Shortest Path Problem Find minimum cost path to from one vertex to another e.g., in an airline map where edges represent flight routes where weights may be distance or fares Edges may be directed or undirected We will assume non-negative weights e.g., an airline map where edges represent flight routes and weights may be distance or fares and we compute the shortest path from A to B

MINIMAL SPANNING TREE PROBLEM Origins Otakar Boruvka, electrical engineer in 1926 most economical construction of electric power network Some modern applications of MST: network layout: telephone, electric, computer, road, cable Has been studied intensely, still looking for faster algorithms

MINIMUM SPANNING TREES (MST) Reminder: Spanning tree ST of graph G(V,E) spanning = all vertices, tree = no cycles ST is a subgraph of G (G'(V,E') where E' E) ST is connected and acyclic Minimum spanning tree MST of graph G MST is a spanning tree of G sum of edge weights is no larger than any other ST Problem: how to (efficiently) find MST for graph G? Make some assumptions: edges in G are not directed (MST for digraphs is harder) no edge weights are negative 9

KRUSKAL S MST ALGORITHM One approach to computing MST for graph G(V,E): start with empty MST consider edges in increasing weight order add edge if it does not form a cycle in MST repeat until V-1 edges are added 10 Critical operations: iterating over edges in weight order checking for cycles in a graph

EXECUTION TRACE OF KRUSKAL S MST 11

KRUSKAL S MST ALGORITHM : PSEUDO-CODE MSTree KruskalMST(Graph g) { MST = empty graph (nv(g)); nconnectedvertices = 0; sortededgelist = sort edges(g) by weight; for each (edge e in sortededgelist) { add e to MST if (MST has a cycle) { remove e from MST; continue; } increment nconnectedvertices; if (nconnectedvertices is equal to nv(g)) break; } return MST } 12

EXERCISE: TRACE KRUSKAL S ALGORITHM 13

KRUSKAL S ALGORITHM: MINIMAL SPANNING TREE Implementation 1: Two main parts: sorting edges according to their length (E * log E) (e.g., quicksort or merge-sort) at least V iterations over sorted edges on each iteration getting next cheapest edge O(1) check if adding the edge would create a cycle Possibilities for cycle checking: using DFS... but too expensive use union-find data structure from Sedgewick ch.1 (~ O(log V) Implementation 2: Using a pq instead of full sort Create a priority queue using weights as priority Allows us to remove edges from pq in weighted order O(E + X *log V), with X = number of edges shorter than the longest edge in the MST

PRIM S ALGORITHM: MINIMAL SPANNING TREE Another approach to computing MST for graph G(V,E): start from any vertex s and empty MST choose edge not already in MST to add to MST must not involve a self-loop must connect to a vertex already on MST must have minimal weight of all such edges repeat until MST covers all vertices Critical operations: checking for vertex being connected in a graph finding min weight edge in a set of edges updating min weights in a set of edges

PRIM S ALGORITHM: MINIMAL SPANNING TREE Pseudo-Code: MSTree PrimMST(Graph g) { MST = empty graph (nv(g)) usedv = {0}; unusede = edges(g) while (size(usedv) < nv(g)) { find (edge e in unusede) satisfying { e = (s,t,w), s usedv, t usedv, w is min weight of all such edges } add e = (s,t,w) to MST usedv += t; unusede -= e } return MST } Critical operation: finding best edge

WEIGHTED GRAPH: MINIMAL SPANNING TREE II Execution trace of Prim s algorithm

EXERCISE: TRACE PRIM S ALGORITHM 18

PRIM S ALGORITHM COST ANALYSIS Implementation 1: Use an adjacency list to represent all the vertices Use a priority queue (implemented as a Min Heap) to extract edge with minimum weight O(Log(E)) Overall time-complexity => O (E *log V) Implementation 2: Use an adjacency matrix to represent all the vertices Overall time-complexity => O(V 2 )

SHORTEST PATHS Path = sequence of edges in graph G ( i, (v i,w i ), w i = v i+1 ) Weight of a path, p (cost(p)) = sum of edge weights along path, p Shortest path between vertices s and t a simple path p where s = first(p), t = last(p) no other simple path q has weight(q) < weight(p) Problem: how to (efficiently) find shortestpath(g,s,t)? Assumptions: weighted graph, no negative weights. Application: robot navigation, routing in data networks

EXERCISE: What is the minimum spanning tree? What is the shortest path from 0 to 3? What is the least hops path (shortest unweighted path) from 0 to 2? 0 3 0.4 0.4 0.1 1 0.3 2 0.2

SHORTEST PATH ALGORITHMS Shortest-path is useful in a wide range of applications robot navigation finding routes in maps routing in data/computer networks Flavours of shortest-path source-target (shortest path from s to t) single-source (shortest paths from s to all other V) all-pairs (shortest paths for all (s,t) pairs)

DIJKSTRA S ALGORITHM SINGLE SOURCE SHORTEST PATHS st

DIJKSTRA S ALGORITHM: SINGLE SOURCE SHORTEST PATH Given: weighted digraph/graph G, source vertex s Result: shortest paths from s to all other vertices dist[] : V-indexed array of distances from s st[] : V-indexed array of predecessors in shortest path Note: shortest paths can be viewed as tree rooted at s

EDGE RELAXATION Relaxation along edge e from v to w dist[v] is length of some path from s to v dist[w] is length of some path from s to w if e gives shorter path s to w via v, then update dist[w] and st[w] Relaxation updates data on w if we find a shorter path to s. st[v] =?, st[w] =? st[v] =?, st[w] = v if (dist[v] + e.weight < dist[w]) { dist[w] = dist[v] + e.weight; st[w] = v; }

DIJKSTRA S ALGORITHM Data: G, s, dist[], st[], and a pq containing the set of vertices whose shortest path from s is not yet known Algorithm: initialise dist[] to all, except dist[s]=0 Initialise pq with all V, with dist[v] as priority v = deletemin from pq Get e s that connect v to w in pq relax along e if new dist is better repeat until pq is empty

EXECUTION TRACE OF DIJKSTRA S ALGORITHM

...EXECUTION TRACE OF DIJKSTRA S ALGORITHN

...EXECUTION TRACE OF DIJKSTRA S ALGORITHM

DIJKSTRA S RESULTS After the algorithm has completed: Shortest Path distances are in dist array Actual path can be traced back from endpoint via the predecessors in the st array

EXERCISE Assume we have just completed running Dijkstra s algorithm with starting vertex v. Write code to print out the path from vertex v to w or No path if the path does not exist. (It is ok to print it in reverse order.)

TRACE EXECUTION OF DIJKSTRA S ALGORITHM FROM STARTING VERTEX 2