• AlgorithmsI Exercises: UnionFind


    Question1 

    Give the id[] array that results from the following sequence of 6 unionoperations on a set of 10 items using the quick-find algorithm. 6-3 2-3 5-3 5-1 9-3 3-0 Your answer should be a sequence of 10 integers, separated by whitespace.Recall: our quick-find convention for the union operation p-q is to change id[p](and perhaps some other entries) but not id[q].

    Answer: 0 0 0 0 4 0 0 7 8 0

    Question Explanation
    The correct answer is: 0 0 0 0 4 0 0 7 8 0

    Here is the id[] array after each union operation:

    0 1 2 3 4 5 6 7 8 9
    6-3: 0 1 2 3 4 5 3 7 8 9
    2-3: 0 1 3 3 4 5 3 7 8 9
    5-3: 0 1 3 3 4 3 3 7 8 9
    5-1: 0 1 1 1 4 1 1 7 8 9
    9-3: 0 1 1 1 4 1 1 7 8 1
    3-0: 0 0 0 0 4 0 0 7 8 0


    Question 2

    Give the id[] array that results from the following sequence of 9 union
    operations on a set of 10 items using the weighted quick-union algorithm from lecture.

    7-9 9-4 7-0 8-1 3-7 5-6 5-1 3-8 2-6

    Your answer should be a sequence of 10 integers, separated by whitespace.

    Recall: when joining two trees of equal size, our weighted quick union convention is to
    make the root of the second tree point to the root of the first tree. Also, our weighted
    quick union algorithm performs union by size (number of nodes) - not union by height -
    and does not do path compression.

    Answer: 7 8 7 7 7 7 5 7 5 7
    Question Explanation
    The correct answer is: 7 8 7 7 7 7 5 7 5 7

    Here is the id[] array after each union operation:

    0 1 2 3 4 5 6 7 8 9
    7-9: 0 1 2 3 4 5 6 7 8 7
    9-4: 0 1 2 3 7 5 6 7 8 7
    7-0: 7 1 2 3 7 5 6 7 8 7
    8-1: 7 8 2 3 7 5 6 7 8 7
    3-7: 7 8 2 7 7 5 6 7 8 7
    5-6: 7 8 2 7 7 5 5 7 8 7
    5-1: 7 8 2 7 7 5 5 7 5 7
    3-8: 7 8 2 7 7 7 5 7 5 7
    2-6: 7 8 7 7 7 7 5 7 5 7


    Question 3

    Which of the following id[] array(s) could be the result of running the weighted quick union
    algorithm on a set of 10 items? Check all that apply.

    Recall that our weighted quick union algorithm uses union by size (number of nodes)
    and not union by height.

    Answer: Choose the last two.

    Your Answer ScoreExplanation

        5 5 5 5 2 6 4 5 2 3 

    Correct 0.20 The id[] array contains a cycle: 6->4->2->5->6
    5 3 6 6 6 5 5 8 6 6 
    Correct 0.20 Size of tree rooted at parent of 6 < twice the size of tree rooted at 6
    8 8 1 1 2 4 1 1 8 9 
    Correct 0.20 Height of forest = 4 > lg N = lg(10)
    6 6 1 6 6 1 6 6 2 6 
    Correct 0.20 6-7 0-7 1-5 2-8 5-8 4-7 6-2 7-3 2-9
    0 2 2 0 4 5 2 7 8 9 
    Correct 0.20 2-1 0-3 2-6
  • 相关阅读:
    关于浏览器及系统的判断
    toggle与slideToggle
    安卓与ios的不同处理
    关于常用循环遍历获取数据
    docker
    Mysql
    rabbitMQ的使用转载
    Git命令行
    vue项目创建完整版
    redis操作(str.hash.list.set)
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4837038.html
Copyright © 2020-2023  润新知