+ All Categories
Home > Documents > Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m...

Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m...

Date post: 30-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
15
Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 [email protected]
Transcript
Page 1: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Data StructuresGiri Narasimhan

Office: ECS 254A Phone: x-3748 [email protected]

Page 2: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

03/11/04 Lecture 18

Page 3: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Vertex and Edge classes Class Edge { public Vertex dest; public double weight; public Edge (Vertex d,

double w) { dest = d; weight = w;

} }

Class Vertex { public String Name; public AnyType extraInfo; public List adj; public int dist; // double? public Vertex prev; public Vertex (String s) { Name = s; adj = new LinkedList(); reset(); } public reset () { dist=INFNT; path=null; } }

10/12/16 COP 3530: DATA STRUCTURES

Page 4: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Topological Sort void topSort () {

for( int j = 0; j < N; j++) { Vertex v = findVertexOfIndegZero(); if (v == null) return; // Cycle found v.topologicalNum = j; for each vertex w adjacent to v w.inDegree--; // use extraInfo field }

} 10/12/16 COP 3530: DATA STRUCTURES Time Complexity = O(n + m)

Page 5: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Unweighted SP algorithm Void BFS (Vertex s) { // same as unweighted SP Queue <Vertex> Q = new Queue <>; for each Vertex v except s { v.dist = INFNT;} s.dist = 0; s.prev = null; Q.enqueue(s); while ( !Q.isEmpty() ) {

v = Q.dequeue(); for each vertex w adjacent to v if (w.dist == INFNT) { w.dist = v.dist + 1; w.prev = v; Q.enqueue(w); }

} }

10/12/16 COP 3530: DATA STRUCTURES Time Complexity = O(n + m)

Page 6: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Dijkstra’s SP algorithm void Dijkstra (Vertex s) { // same as weighted SP PriorityQueue <Vertex> Q = new PriorityQueue <>; for each Vertex v except s { v.dist = INFNT; Q.insert(v); } s.dist = 0; s.prev= null; Q.insert(s); while ( !Q.isEmpty() ) {

v = Q.deleteMin(); for each vertex w adjacent to v if (w.dist > v.dist + weight of edge (v,w)) { w.dist = v.dist + weight of edge (v,w); w.prev= v; Q.updatePriority(w, v.dist + weight of edge (v,w)); }

} }

10/12/16 COP 3530: DATA STRUCTURES Time Complexity = O(n log n + m + m log n) = O(m log n)

Page 7: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Spanning Trees and MST u  Graph G(V,E)

u  Path: sequence of edges

u  Cycle: closed path

u  A subgraph G’ of G is given by G’(V’, E’)

u  A tree is a graph with no cycles

u  A connected graph has a path between all vertices

u  A spanning tree is a tree that connects all vertices

10/12/16 COP 3530: DATA STRUCTURES

Page 8: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Minimum Spanning Trees u  Input: Weighted Graph, G(V,E) with n vertices & m

edges, and edge weights w(e) for edge e

u  Output: Find a spanning tree of minimum total weight

10/12/16 COP 3530: DATA STRUCTURES

Page 9: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Observations and Facts u  Every spanning tree has n-1 edges.

u  A spanning tree connects all vertices of a graph.

u  A spanning tree has no cycles.

u  If there is a cycle in the original graph, any spanning tree must miss at least one edge. q  MST misses one heaviest edge on that cycle.

u  Given any partition of vertices into (S, V-S), every spanning tree must include at least one edge from S to V-S. q  MST includes one lightest edge from E(S,V-S).

10/12/16 COP 3530: DATA STRUCTURES

Page 10: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

MST – Prim’s Algorithm 11/16/2016 Bookshelf Online: Data Structures and Algorithm Analysis in Java

https://bookshelf.vitalsource.com/#/books/9780133465013/cfi/6/212!/4/2/6/2/2@0:0 3/4

Figure 9.50 A graph G and its minimum spanning tree

This shows that greed works for the minimum spanning tree problem. The two algorithms we

present differ in how a minimum edge is selected.

10/12/16 COP 3530: DATA STRUCTURES

Page 11: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Prim’s Algorithm

11/17/2016 Bookshelf Online: Data Structures and Algorithm Analysis in Java

https://bookshelf.vitalsource.com/#/books/9780133465013/cfi/6/32!/4/2/6/10/14/10/2@0:73.8 1/2

Figure 9.51 Prim’s algorithm after each stage

Figure 9.52 Initial configuration of table used in Prim’s algorithm

 

PRINTED BY: [email protected]. Printing is for personal, private use only. No part of this book may be reproduced ortransmitted without publisher's prior permission. Violators will be prosecuted.

10/12/16 COP 3530: DATA STRUCTURES

Page 12: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Reminder of Dijkstra’s Alg void Dijkstra (Vertex s) { // same as weighted SP PriorityQueue <Vertex> Q = new PriorityQueue <>; for each Vertex v except s { v.dist = INFNT; Q.insert(v); } s.dist = 0; s.prev= null; Q.insert(s); while ( !Q.isEmpty() ) {

v = Q.deleteMin(); for each vertex w adjacent to v if (w.dist > v.dist + weight of edge (v,w)) { w.dist = v.dist + weight of edge (v,w); w.prev= v; Q.updatePriority(w, v.dist + weight of edge (v,w)); }

} }

10/12/16 COP 3530: DATA STRUCTURES Time Complexity = O(n log n + m + m log n) = O(m log n)

Page 13: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Prim’s Algorithm ArrayList <Edges> Prim (Vertex s) { // computes MST PriorityQueue <Vertex> Q = new PriorityQueue <>; for each Vertex v except s { v.dist = INFNT; Q.insert(v); } s.dist = 0; s.prev= null; Q.insert(s); while ( !Q.isEmpty() ) {

v = Q.deleteMin(); for each vertex w adjacent to v if (w.dist > weight of edge (v,w)) { w.dist = weight of edge (v,w); w.prev= v; Q.updatePriority(w, weight of edge (v,w)); }

} for each Vertex v except s { compile List mst with edges (v.prev, v); } return mst; }

10/12/16 COP 3530: DATA STRUCTURES Time Complexity = O(n log n + m + m log n) = O(m log n)

Page 14: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Kruskal’s Algorithm

11/17/2016 Bookshelf Online: Data Structures and Algorithm Analysis in Java

https://bookshelf.vitalsource.com/#/books/9780133465013/cfi/6/32!/4/2/6/10/16/12/4/2/4/16/2/2@0:0 1/2

Figure 9.58 Action of Kruskal’s algorithm on G

Figure 9.59 Kruskal’s algorithm after each stage

vertex is initially in its own set. If u and v are in the same set, the edge is rejected, because

since they are already connected, adding (u, v) would form a cycle. Otherwise, the edge is

accepted, and a union is performed on the two sets containing u and v. It is easy to see

that this maintains the set invariant, because once the edge (u, v) is added to the spanning

forest, if w was connected to u and x was connected to v, then x and w must now be

connected, and thus belong in the same set.

The edges could be sorted to facilitate the selection, but building a heap in linear time is a

much better idea. Then deleteMins give the edges to be tested in order. Typically, only a

small fraction of the edges need to be tested before the algorithm can terminate, although

 

PRINTED BY: [email protected]. Printing is for personal, private use only. No part of this book may be reproduced ortransmitted without publisher's prior permission. Violators will be prosecuted.

 

PRINTED BY: [email protected]. Printing is for personal, private use only. No part of this book may be reproduced or

10/12/16 COP 3530: DATA STRUCTURES

Page 15: Data Structuresgiri/teach/3530/f16/Lectures/...2004/03/11  · Time Complexity = O(n log n + m + m log n) = O(m log n) COP 3530: DATA STRUCTURES 10/12/16 Kruskal’s Algorithm 11/17/2016

Pseudocode: Kruskal’s ArrayList <Edges> Kruskal( List <Edge> edges, int N) {

PriorityQueue<Edge> Q = new PriorityQueue<>(edges); List<Edge> mst = new ArrayList<>(); while mst.size() != N-1) { Edge e = Q.deleteMin(); if (EdgeConnectsDifferentComponents(e)) { mst.add(e); UpdateComponents(e); } } return mst;

}

10/12/16 COP 3530: DATA STRUCTURES

Time Complexity = O(m log m) = O(m log n)


Recommended