• Tidak ada hasil yang ditemukan

Data Structure

N/A
N/A
Protected

Academic year: 2024

Membagikan "Data Structure"

Copied!
14
0
0

Teks penuh

(1)

Data Structure

Lecture#25: Graphs 3 (Chapter 11)

U Kang

Seoul National University

(2)

U Kang (2016) 2

In This Lecture

MST (Minimum Spanning Tree) problem

Main idea and cost of Prim’s algorithm for MST

Main idea and cost of Kruskal’s algorithm for

MST

(3)

Minimum Cost Spanning Trees (1)

Minimum Cost Spanning Tree (MST) Problem:

Input: An undirected, connected graph G.

Output: The subgraph of G that

1) has minimum total cost as measured by summing the values of all the edges in the subset, and

2) keeps the vertices connected.

(4)

U Kang (2016) 4

Minimum Cost Spanning Trees (2)

Minimum Cost Spanning Tree (MST) Problem:

A tree means a graph without cycle

Property 1) of the output ensures MST has no cycle (why?)

Property 1) G has minimum total cost as measured by summing the values of all the edges in the subset

(5)

Minimum Cost Spanning Trees (3)

Minimum Cost Spanning Tree (MST) Problem:

Solution may not be unique

E.g., (C,F) can be replaced with (D,F) in the previous slide

But, the minimum costs are the same for all the solutions

MST algorithms

Prim’s algorithm

Kruskal’s algorithm

(6)

U Kang (2016) 6

Prim’s MST Algorithm (1)

Start with any vertex N in the graph. Add N to MST.

Pick the least-cost edge (N,M) connected to N.

Add vertex M and edge (N,M) to the MST.

Pick the least-cost edge connected to (N or M). Let the vertex X be connected to the edge.

X should not belong to the current MST.

Add vertex X and the edge to the MST.

(continue, until all vertices are added to MST)…

(7)

Prim’s MST Algorithm (2)

A B C D E F

Initial ∞ ∞ 0 ∞ ∞ ∞

Process C 7 5 0 1 2

Process D 7 5 0 1 2

Process F 7 5 0 1 1 2

Process E 7 5 0 1 1 2

Process B 7 5 0 1 1 2

Process A 7 5 0 1 1 2

(8)

U Kang (2016) 8

Prim’s MST Algorithm (3)

// Compute a minimal-cost spanning tree

void Prim(Graph G, int s, int[] D, int[] V) { int v, w;

for (int i=0; i<G.n(); i++) // Initialize D[i] = Integer.MAX_VALUE;

D[s] = 0;

for (int i=0; i<G.n(); i++) { v = minVertex(G, D);

G.setMark(v, VISITED);

if (v != s) AddEdgetoMST(V[v], v);

if (D[v] == Integer.MAX_VALUE) return;

for (w=G.first(v); w<G.n(); w=G.next(v, w)) if (D[w] > G.weight(v, w)) {

D[w] = G.weight(v, w);

V[w] = v;

} }

}

(9)

Running Time of Prim’s MST

Prim’s MST is very similar to Dijkstra’s algorithm

MST:

D[w] = G.weight(v, w);

Shortest Path:

D[w] = D[v] + G.weight(v, w);

So does the running time of Prim’s MST

Scan through the distance table: Θ(|V|2 + |E|) = Θ(|V|2)

Min-heap: Θ((|V| + |E|)log|V|)

(10)

U Kang (2016) 10

Kruskal’s MST Algorithm (1)

Initially, each vertex is in its own MST.

Merge two MST’s that have the shortest edge between them.

Use a priority queue to order the unprocessed edges.

Grab next one at each step.

Make sure the edge does not connect two vertices in a same MST

How to tell if an edge connects two vertices already in the same MST?

Use the UNION/FIND algorithm with parent-pointer representation.

(11)

Kruskal’s MST Algorithm (2)

(12)

U Kang (2016) 12

Kruskal’s MST Algorithm (3)

Cost is dominated by the time to remove edges from the heap.

Can stop processing edges once all vertices are in the same MST

Total cost: Θ(|E| log |E|)

Can remove edges |E| times

(13)

Summary

MST (Minimum Spanning Tree) problem

Main idea and cost of Prim’s algorithm for MST

Cost the same as that of Dijkstra’s

Main idea and cost of Kruskal’s algorithm for

MST

(14)

U Kang (2016) 14

Questions?

Referensi

Dokumen terkait

Berdasarkan hasil penelitian yang dilakukan, maka dapat ditarik kesimpulan bahwa metode Minimum Spanning Tree (MST) dengan menggunakan Algoritma kruskal yang diringkas menggunakan

Berdasarkan hasil penelitian yang dilakukan, maka dapat ditarik kesimpulan bahwa metode Minimum Spanning Tree (MST) dengan menggunakan Algoritma kruskal yang diringkas menggunakan

Perhitungan minimum spanning tree algoritma Kruskal dan algoritma Prim dimulai Tabel 2 yang merupakan urutan jarak terpendek dari pendistribusian gas 3 Kg

Berdasarkan penelitian pengujian sistem dari gambar running time dari minimum spanning tree dengan algoritma Kruskal dan algoritma Prim dapat di ketahui dari

Selanjutnya, membentuk graf tersebut menjadi Minimum Spanning Tree MST menggunakan algoritma Kruskal, dengan tujuan meminimalkan biaya pada permasalahan transportasi dan desain jaringan

What you need to know  Asymptotic analysis  Analyze the time and space complexity of an algorithm  Time/space tradeoff  Understand the main idea  Convert an operation to trade

In This Lecture  Learn the motivation and main idea of doubly linked list  Learn the Stack and Queue data structure  Learn the Dictionary data structure... Limitation of Linked

What you need to know  Heapsort: main idea and advantage  Useful for finding k largestsmallest items  Binsort: main idea and advantage/disadvantage  Depends on MaxKeyValue 