Lecture 8 Greedy algorithms: Priority Queues - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Lecture 8 Greedy algorithms: Priority Queues

Description:

Output: finding the spanning tree such that the sum of weights is not bigger ... Prove that a spanning tree of n - node graph has n - 1 edges ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 16
Provided by: DarekKo5
Category:

less

Transcript and Presenter's Notes

Title: Lecture 8 Greedy algorithms: Priority Queues


1
Lecture 8 Greedy algorithmsPriority Queues
  • COMP 523 Advanced Algorithmic Techniques
  • Lecturer Dariusz Kowalski

2
Overview
  • Previous lectures
  • Greedy algorithms
  • Minimum spanning tree - many greedy approaches
    (Prims and Kruskals algorithms)
  • This lecture
  • Priority Queues
  • Implementation of Prims algorithm using PQ

3
Minimum spanning tree
  • Input weighted graph G (V,E)
  • every edge in E has its positive weight
  • Output finding the spanning tree such that the
    sum of weights is not bigger than the sum of
    weights of any other spanning tree
  • Spanning tree subgraph with
  • no cycle, and
  • connected (every two nodes in V are connected by
    a path)

2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
4
Crucial observation about MST
  • Consider sets of nodes A and V - A
  • Let F be the set of edges between A and V - A
  • Let a be the smallest weight of an edge from F
  • Theorem
  • Every MST must contain at least one edge of
    weight a
  • from set F

A
A
2
2
1
1
1
1
2
2
3
3
5
Greedy algorithm finding MST
  • Prims algorithm
  • Select a node as a root arbitrarily
  • Choose n - 1 edges one after another as follows
  • Look on all edges incident to the currently build
    (partial) tree and which do not create a cycle in
    it, and select one which has the smallest weight
  • Remark we always have a connected partial tree

root
2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
6
Priority queue
  • Set of n elements, each has its priority value
    (key).
  • Operations provided in time O(log n)
  • Adding new element
  • Removing an element from the PQ
  • Taking element with smallest key
  • The smaller key the higher priority the element
    has.

7
Implementation of PQ based on heaps
  • Heap rooted (almost) complete binary tree, each
    node has
  • its value,
  • key,
  • 3 pointers to the parent and children (or nil if
    parent or children not available)
  • Required property
  • in each subtree the smallest key is always in the
    root

2
4
3
6
5
7
2
3
4
7
5
6
8
Construction of heap
  • Construction
  • Start with arbitrary element, and add next
    elements using
  • adding operation provided by the heap data
    structure
  • (which will be defined in the next slide)

9
Implementing operations on heap
  • Implementing operations
  • Smallest key element trivially from the root
  • Adding new element put it as the last leaf in
    the heap and recursively compare it with its
    parents key if the element has the smaller key
    then swap the element and its parent and continue
  • Remark putting the new element as the last leaf
    may require to search through the path for the
    element to connect the last leaf
  • Removing element remove it from the tree, move
    the last leaf on its place, compare this moved
    element recursively either
  • up if its value is smaller than its current
    parent - swap the elements and continue going up
    until reaching smaller parent or the root, or
  • down if its value is smaller than one of its
    current children - swap it with the smallest of
    its children and continue going down until
    reaching no smaller children or a leaf

10
Examples - adding
2
2
4
3
1
3
6
5
7
1
6
5
7
4
add 1 at the end
swap 1 and 4
2
3
4
7
5
6
1
2
3
1
7
5
6
4
1
2
3
1
3
2
7
5
6
4
swap 1 and 2
6
5
7
4
11
Examples - removing
2
6
3
4
3
4
3
4
6
6
5
7
5
7
5
7
remove 2 and swap 6 and 3
removing 2
swap 2 and last element
2
3
4
7
5
6
3
4
7
5
6
6
4
7
5
3
3
4
5
5
4
7
6
3
swap 6 and 5
6
7
12
Heap operations - time complexity
  • Taking minimum O(1)
  • Adding
  • Finding last leaf O(log n)
  • Going up with swaps through (almost) complete
    binary tree O(log n)
  • Removing
  • Finding last leaf and moving it O(1)
  • Going up or down (only once direction is
    selected) with swaps through (almost) complete
    binary tree O(log n)

13
Prims algorithm - time complexity
  • Select a root node
  • Construct PQ with all edges incident to the root
  • Repeat n - 1 times
  • Select the smallest edge from PQ
  • Add its end to the partial tree
  • Add all edges incident to the new node to PQ
  • Time complexity O(m log n)
  • where n is the number of nodes and m is the
    number of edges

14
Conclusions
  • Priority Queues
  • Greedy Prims algorithms for finding minimum
    spanning tree in a graph, both in time O(m log n)

15
Exercises
  • Solved Exercises 1 and 2 from textbook - Chapter
    4 Greedy Algorithms
  • Prove that a spanning tree of n - node graph has
    n - 1 edges
  • Prove that an n - node connected graph has at
    least n - 1 edges
Write a Comment
User Comments (0)
About PowerShow.com