CSC263 Week 10. Larry Zhang.

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

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,

Social Media Mining. Graph Essentials

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Analysis of Algorithms, I

Approximation Algorithms

CS711008Z Algorithm Design and Analysis

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

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

Connectivity and cuts

SCAN: A Structural Clustering Algorithm for Networks

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

CMPSCI611: Approximating MAX-CUT Lecture 20

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

CIS 700: algorithms for Big Data

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.

Exam study sheet for CS2711. List of topics

Chapter 6: Graph Theory

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

6 March Array Implementation of Binary Trees

Minimum Spanning Trees

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.

Cpt S 223. School of EECS, WSU

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)

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

Strategic Deployment in Graphs. 1 Introduction. v 1 = v s. v 2. v 4. e 1. e e 3. e 2. e 4

Algorithms and Data Structures

Solutions to Homework 6

Binary Search Trees CMPSC 122

Algorithm Design and Analysis

Data Structures and Algorithms Written Examination

1 Approximating Set Cover

CSC 505, Fall 2000: Week 8

Finding and counting given length cycles

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

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

Institut für Informatik Lehrstuhl Theoretische Informatik I / Komplexitätstheorie. An Iterative Compression Algorithm for Vertex Cover

SEMITOTAL AND TOTAL BLOCK-CUTVERTEX GRAPH

1. Nondeterministically guess a solution (called a certificate) 2. Check whether the solution solves the problem (called verification)

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

Approximated Distributed Minimum Vertex Cover Algorithms for Bounded Degree Graphs

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

MATHEMATICS Unit Decision 1

Well-Separated Pair Decomposition for the Unit-disk Graph Metric and its Applications

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

Algorithms and data structures

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

International Journal of Software and Web Sciences (IJSWS)

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Bicolored Shortest Paths in Graphs with Applications to Network Overlay Design

GRAPH THEORY LECTURE 4: TREES

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

Every tree contains a large induced subgraph with all degrees odd

Problem Set 7 Solutions

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Exponential time algorithms for graph coloring

MapReduce and Distributed Data Analysis. Sergei Vassilvitskii Google Research

Attacking Anonymized Social Network

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

MATHEMATICS Unit Decision 1

Part 2: Community Detection

Graph Theory Problems and Solutions

Page 1. CSCE 310J Data Structures & Algorithms. CSCE 310J Data Structures & Algorithms. P, NP, and NP-Complete. Polynomial-Time Algorithms

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Scheduling Shop Scheduling. Tim Nieberg

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Max Flow, Min Cut, and Matchings (Solution)

Graph Theory and Complex Networks: An Introduction. Chapter 06: Network analysis. Contents. Introduction. Maarten van Steen. Version: April 28, 2014

SPANNING CACTI FOR STRUCTURALLY CONTROLLABLE NETWORKS NGO THI TU ANH NATIONAL UNIVERSITY OF SINGAPORE

Binary Heaps. CSE 373 Data Structures

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Analysis of Algorithms I: Binary Search Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Network File Storage with Graceful Performance Degradation

Cpt S 223. School of EECS, WSU

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Distributed Computing over Communication Networks: Maximal Independent Set

Definition Given a graph G on n vertices, we define the following quantities:

Small Maximal Independent Sets and Faster Exact Graph Coloring

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

INTERSECTION OF LINE-SEGMENTS

Full and Complete Binary Trees

A simpler and better derandomization of an approximation algorithm for Single Source Rent-or-Buy

Euler Paths and Euler Circuits

Shortest Inspection-Path. Queries in Simple Polygons

Converting a Number from Decimal to Binary

Data Structures Fibonacci Heaps, Amortized Analysis

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Outline 2.1 Graph Isomorphism 2.2 Automorphisms and Symmetry 2.3 Subgraphs, part 1

Cluster Editing And Search Tree Algorithms

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

8.1 Min Degree Spanning Tree

Algorithm Homework and Test Problems

Graph Theory and Complex Networks: An Introduction. Chapter 06: Network analysis

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Lecture 17 : Equivalence and Order Relations DRAFT

arxiv: v1 [cs.dc] 7 Dec 2014

GENERATING LOW-DEGREE 2-SPANNERS

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

Topology-based network security

Transcription:

CSC263 Week 10 Larry Zhang http://goo.gl/forms/s9yie397b

Announcement PS8 out soon, due next Tuesday

Minimum Spanning Tree

The Graph of interest today A connected undirected weighted graph G = (V, E) with weights w(e) for each e E 8 2 10 3 12

It has the smallest total weight It covers all vertices in G Minimum Spanning Tree of graph G It s a connected, acyclic subgraph

A Minimum Spanning Tree 8 2 10 3 12 May NOT be unique

Applications of MST Build a road network that connects all towns and with the minimum cost.

Applications of MST Connect all components with the least amount of wiring.

Other applications Cluster analysis Approximation algorithms for the travelling salesman problem...

In order to understand minimum spanning tree we need to first understand tree

Tree: undirected connected acyclic graph A tree T with n vertices has n-1 exactly edges. Removing one edge from T disconnect the tree will. Adding one edge to T will create a cycle.

The MST of a connected graph G = (V, E) V has vertices. because spanning The MST of a connected graph G = (V, E) V - 1 has edges. because tree

Now we are ready to talk about algorithms

Idea #1 Start with T = G.E, then keep deleting edges until an MST remains. Which sounds more efficient in terms of worst-case runtime? Idea #2 Start with empty T, then keep adding edges until an MST is built.

Hint A undirected simple graph G with n vertices can have at most edges.

Note: Here T is an edge set Idea #1 Start with T = G.E, then keep deleting edges until an MST remains. In worst-case, need to delete O( V ²) edges (n choose 2) - (n-1) Idea #2 In worst-case, need to add O( V ) edges Start with empty T, then keep adding edges until an MST is built. This is more efficient!

So, let s explore more of Idea #2, i.e., building an MST by adding edges one by one i.e., we grow a tree

The generic growing algorithm GENERIC-MST(G=(V, E, w)): T T < V -1 while T is not a spanning tree: find a safe edge e T T {e} return T What is a safe edge?

Safe means it keeps the hope of T growing into an MST. Safe edge e for T Assuming before adding e, T some MST, edge e is safe if after adding e, still T some MST If we make sure T is always a subset of some MST while we grow it, then eventually T will become an MST! GENERIC-MST(G=(V, E, w)): T while T is not a spanning tree: find a safe edge e T T {e} return T

Intuition If we make sure the pieces we put together is always a subset of the real picture while we grow it, then eventually it will become the real picture!

The generic growing algorithm GENERIC-MST(G=(V, E, w)): T T < V -1 while T is not a spanning tree: find a safe edge e T T {e} return T How to find a safe edge?

Two major algorithms we ll learn Kruskal s algorithm Prim s algorithm They are both based on one theorem...

Note: Here T includes both vertices and edges The Theorem Let G be a connected undirected weighted graph, and T be a subgraph of G which is a subset of some MST of G. Let edge e be the minimum weighted edge among all edges that cross different connected components of T. Then e is safe for T.

Initially, T (red) is a subgraph with no edge, each vertex is a connected component, all edges are crossing components, and the minimum weighted one is... a 8 SAFE! b 10 e 2 3 d c 12

Now b and c in one connected component, each of the other vertices is a component, i. e., 4 components. All gray edges are crossing components. a 8 b 10 e 2 3 d c 12 SAFE!

Now b, c and d are in one connected component, a and e each is a component. (c, d) is NOT crossing components! a SAFE! 8 b 10 e 2 3 d c 12 ALSO SAFE!

Now b, c, d and e are in one connected component, a is a component. (a, e) and (a, b) are crossing components. SAFE! a 8 b 10 e 2 3 d c 12

MST grown! a 8 b 10 e 2 3 d c 12

Two things that need to be worried about when actually implementing the algorithm How to keep track of the connected components? How to efficiently find the minimum weighted edge? Kruskal s and Prim s basically use different data structures to do these two things.

to be continued...

CSC263 Week 10 Thursday

Recap: Generic MST growing algorithm GENERIC-MST(G=(V, E, w)): T while T is not a spanning tree: find a safe edge e T T {e} return T

Recap: Finding safe edge Let G be a connected undirected weighted graph, and T be a subgraph of G which is a subset of some MST of G. Let edge e be the minimum weighted edge among all edges that cross different connected components of T. Then e is safe for T.

Recap a 8 b 10 e 2 3 d c 12 SAFE!

Two things that need to be worried about when actually implementing the algorithm How to keep track of the connected components? How to efficiently find the minimum weighted edge? Kruskal s and Prim s basically use different data structures to do these two things.

Overview: Prim s and Kruskal s Keep track of Find minimum connected weight edge components Prim s Kruskal s Keep one tree plus isolated vertices use priority queue ADT use disjoint set ADT Sort all edges according to weight

Prim s Kruskal s https://trendsofcode.files.wordpress.com/2014/09/dijkstra.gif https://www.projectrhea.org/rhea/images/4/4b/kruskal_old_kiwi.gif

Prim s MST algorithm

Prim s algorithm: Idea Start from an arbitrary vertex as root Focus on growing one tree, add one edge at a time. The tree is one component, each of the other (isolated) vertices is a component. Add which edge? Among all edges that are incident to the current tree (edges crossing components), pick one with the minimum weight. How to get that minimum? Store all candidate vertices in a Min-Priority Queue whose key is the weight of the crossing edge (incident to tree).

PRIM-MST(G=(V, E, w)): key[v] keeps the shortest distance 1 T {} between v and the current tree 2 for all v in V: 3 key[v] pi[v] keeps who, in the tree, is v connected to via lightest edge. 4 pi[v] NIL Initialize priority queue Q with all v in V 6 pick arbitrary vertex r as root 7 key[r] 0 u is the next vertex to add to 8 while Q is not empty: current tree 9 u EXTRACT-MIN(Q) add edge, pi[u] is lightest 10 if pi[u]!= NIL: vertex to connect to, safe 11 T T {(pi[u], u)} 12 for each neighbour v of u: 13 if v in Q and w(u, v) < key[v]: 14 DECREASE-KEY(Q, v, w(u, v)) all u s neighbours distances to the 1 pi[v] u current tree need update

Trace an example! Q Pick a as root a 8 b 3 e 2 10 d c key pi a 0 NIL b NIL c NIL d NIL e NIL 12 Next, ExtractMin!

ExtractMin (#1) then update neighbours keys a: 0, NIL a 8 Q b 3 e 2 10 d c key pi 8 NIL a b c NIL d NIL e 3 NIL 12 a

ExtractMin (#2) then update neighbours keys e: 3, a a 8 Q b 3 e 2 10 d c key pi b 8 a c d e NIL 12 NIL e

ExtractMin (#3) then update neighbours keys b:, e a 8 Q b 3 e 2 10 d c key pi c 2 NIL d b e 12 Could also have extracted d since its key is also (min)

ExtractMin (#4) then update neighbours keys c: 2, b a 8 Q b 3 e 2 10 d d c 12 key pi e

ExtractMin (#4) then update neighbours keys d:, e a 8 Q b 3 e pi 2 10 d key c Q is empty now. 12 MST grown!

Correctness of Prim s The added edge is always a safe edge, i.e., the minimum weight edge crossing different components (because ExtractMin). a 8 b 3 e 2 10 d c 12

Runtime analysis: Prim s Assume we use binary min heap to implement the priority queue. Each ExtractMin take O(log V) In total V ExtractMin s In total, check at most O(E) neighbours, each check neighbour could lead to a DecreaseKey which takes O(log V) TOTAL: O( (V+E)log V ) = O(E log V)

In a connected graph G = (V, E) V is in O( E ) because E has to be at least V -1 Also, log E is in O(log V ) because E is at most V², so log E is at most log V² = 2 log V, which is in O(log V)

Kruskal s MST algorithm

Kruskal s algorithm: idea Sort all edges according to weight, then start adding to MST from the lightest one. This is greedy! Constraint: added edge must NOT cause a cycle In other words, the two endpoints of the edge must belong to two different trees (components). The whole process is like unioning small trees into a big tree.

Pseudocode m = E KRUSKAL-MST(G(V, E, w)): 1 T {} 2 sort edges so that w(e1) w(e2)... w(em) 3 for i 1 to m: 4 # let (ui, vi) = ei if ui and vi in different components: 6 T T {ei}

Example a 6 b 3 e 9 2 10 d c 12

Add (b, c), the lightest edge a 6 b 3 e 9 2 10 d c 12

Add (a, e), the 2nd lightest a 6 b 3 e 9 2 10 d c 12

Add (b, e), the 3rd lightest a 6 b 3 e 9 2 10 d c 12

Add (a, b), the 4th lightest... No! a, b are in the same component Add (d, e) instead! a 6 b 3 e 9 2 10 d c 12

MST grown! Add (d, e)... a 6 b 3 e 9 2 10 d c 12

Correctness of Kruskal s The added edge is always a safe edge, because it is the minimum weight edge among all edges that cross components a 6 b 3 e 9 2 10 d c 12

Runtime... m = E sorting takes O(E log E) KRUSKAL-MST(G(V, E, w)): 1 T {} 2 sort edges so that w(e1) w(e2)... w(em) 3 for i 1 to m: 4 # let (ui, vi) = ei if ui and vi in different components: 6 T T {ei} How exactly do we do this two lines?

We need the Disjoint Set ADT which stores a collections of nonempty disjoint sets S1, S2,, Sk, each has a representative. and supports the following operations MakeSet(x): create a new set {x} FindSet(x): return the representative of the set that x belongs to Union(x, y): union the two sets that contain x and y, if different

Real Pseudocode m = E KRUSKAL-MST(G(V, E, w)): 1 T {} 2 sort edges so that w(e1) w(e2)... w(em) 3 for each v in V: 4 MakeSet(v) for i 1 to m: 6 # let (ui, vi) = ei 7 if FindSet(ui)!= FindSet(vi): 8 Union(ui, vi) 9 T T {ei}

Next week More on Disjoint Set 97B http://goo.gl/forms/s9yie3