13
Algorithms and Data Structures Solutions to Assignment 1 (only for the non-programming tasks) 1

Assignment1 Solutions

Embed Size (px)

DESCRIPTION

Algorithms and Data Structures

Citation preview

  • Algorithms and Data Structures

    Solutions to Assignment 1 (only for the non-programming tasks)

    1

  • Task 1

    0-2 0-2

    1-4 1-4

    2-5 2-5

    3-6 3-6

    0-4 0-4

    6-0 6-0

    1-3 is connected (throught 1-4-0-6-3)

    input output

    2

  • Task 2

    We do not provide the code for the solution.

    However, the key idea is this:

    At the beginning, there are N sets remaining.

    Every time we do a union, the number of remaining sets decreases by 1.

    3

  • Task 3

    p q 0 1 2 3 4 5 6

    Number of times

    the id array is

    accessed

    0 2 2 1 2 3 4 5 6 10005

    1 4 2 4 2 3 4 5 6 10005

    2 5 5 4 5 3 4 5 6 10008

    3 6 5 4 5 6 4 5 6 10005

    0 4 4 4 4 6 4 4 6 10011

    6 0 4 4 4 4 4 4 4 10008

    1 3 4 4 4 4 4 4 4 2

    4

  • Task 4 Figure 1.7

    p q 0 1 2 3 4 5 6 7 8 9

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

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

    8 0 8 1 2 3 3 5 6 7 8 3

    2 3 8 1 3 3 3 5 6 7 8 3

    5 6 8 1 3 3 3 5 5 7 8 3

    5 9 8 1 3 3 3 3 5 7 8 3

    7 3 8 1 3 3 3 3 5 3 8 3

    4 8 8 1 3 3 3 3 5 3 3 3

    6 1 8 3 3 3 3 3 5 3 3 3

    5

  • Task 4 Figure 1.8

    p q 0 1 2 3 4 5 6 7 8 9

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

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

    4 5 0 0 2 2 4 4 6 7 8 9

    6 7 0 0 2 2 4 4 6 6 8 9

    8 9 0 0 2 2 4 4 6 6 8 8

    0 2 0 0 0 2 4 4 6 6 8 8

    4 6 0 0 0 2 4 4 4 6 8 8

    0 4 0 0 0 2 0 4 4 6 8 8

    0 8 0 0 0 2 0 4 4 6 0 8

    6

  • Task 5

    p q 0 1 2 3 4 5 6 # times

    0 2 0 1 0 3 4 5 6 3

    1 4 0 1 0 3 1 5 6 3

    2 5 0 1 0 3 1 0 6 5

    3 6 0 1 0 3 1 0 3 3

    0 4 0 0 0 3 1 0 3 5

    6 0 0 1 0 0 1 0 3 5

    1 3 0 0 0 0 1 0 3 6

    7

  • Task 6

    M=106 input pairs

    N=109 nodes

    Assumption: each input pair leads to a union (otherwise, in the extreme case, if no input pair leads to a union, the running time is 0 days).

    F = Minimum number of instructions to execute one iteration of a for loop execution = 10 instructions

    C = 109 instructions/sec

    Formula = (M*N*F)/C = 1016 / 109 =107 sec

    115.74 days (1 day = 86400 sec)

    8

  • Task 7

    M=106 input pairs

    N=109 nodes

    W = maximum number of instructions to execute each iteration of a while loop = 100 instructions

    C = 109 instructions/sec

    Formula = (M * W) / C = 106 x 100 / 109 = 10-1 second

    9

  • Task 8

    Assume: length 4 4 nodes + 3 edges.

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

    5

    8 4 7 6

    0 9 1 3

    2 10

  • Task 10 Step 1

    0

    1

    2

    3

    4

    5

    6

    7

    Connect:

    1 to 0

    3 to 2

    5 to 4

    7 to 6

    11

  • Task 10 Step 2

    0

    1 2

    3

    4

    5 6

    7

    Connect:

    2 to 0

    6 to 4

    12

  • Task 10 Step 3

    0

    1 2

    3

    4

    5 6

    7

    Connect 4 to 0

    13