63
Solution for Tutorial 11 (updated Apr 2018)

Solution for Tutorial 11 (updated Apr 2018) · 2019. 6. 25. · Solution for Tutorial 11 (updated Apr 2018) EE2008 11. Solution for Tutorial 9 . EE2008. 3 EE2008 11. EE2008 4. EE2008

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

  • Solution for Tutorial 11

    (updated Apr 2018)

  • EE2008

    11

  • Solution for Tutorial 9

  • EE2008

  • EE20083

    11

  • EE20084

  • EE20085

  • Solution for Tutorial 12

  • EE2008

  • EE2008

  • • Q3

  • • Q3

  • T11-Q4. Write a stack based non-recursive algorithm

    implementation of the depth-first-search approach.

    [Reference - Lecture notes]

    Q4 Answer:

    dfs(adj, start){

    n=adj.last

    for i=1 to n

    visit[i]=false

    visit[start]=true

    println(start)

    s.push(start) // s is an initially empty stack & push a start vertex.

    while(!s.empty()){

    v=adj[s.top()]

    while(v!=NULL){

    if (!visit[v]){

    s.push(v)

    visit[v]=true

    println(v)

    break

    }

    else

    v=v.next

    }

    if (v==NULL) s.pop()

    }

    }

  • T11-Q4. Write a stack based non-recursive algorithm

    implementation of the depth-first-search approach.

    [Reference - Lecture notes]

    Q4 Answer:

    dfs(adj, start){

    n=adj.last

    for i=1 to n

    visit[i]=false

    visit[start]=true

    println(start)

    s.push(start) // s is an initially empty stack & push a start vertex.

    while(!s.empty()){

    v=adj[s.top()]

    while(v!=NULL){

    if (!visit[v]){

    s.push(v)

    visit[v]=true

    println(v)

    break

    }

    else

    v=v.next

    }

    if (v==NULL) s.pop()

    }

    }

    s.pop to return previously

    visited node

  • Solution for Tutorial 13

  • EE2008

    Nodes Indegree

    1 0

    2 2

    3 1

    4 4

  • EE2008

    Nodes Indegree

    1 0 √

    2 2 1

    3 1 0

    4 4 3

  • EE2008

  • EE2008

  • EE2008

  • EE2008

  • EE2008

  • EE2008

  • EE2008

  • EE2008

  • EE2008

  • Topological Sorts are applied

    in Excel Spreadsheets to

    detect cycles

  • EE2008

  • EE2008

  • EE20084

  • • Q5

  • End of Tutorial Slides

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    Solution for Tutor ial 11.1. Find in the following ar r ay the index of the element of 22 by applying the binary sear ch algor ithm. How many times is the binary

    search algor ithm faster than the linear search in this case?

    ©SBH/ AY2017-18

    Answer :Iteration 1:

    Low Mid High

    Iteration 2:

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    Low Mid HighIteration 3:

    Low Mid High

    Iteration 4:

    Low=Mid=High

    The index of the element is 11.After 4 times binary searching, the result has been found; however, 11 times may be required to find such result by using linear method. Thus, in this case, binary search is 3 times faster than linear method.

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

  • EE2008Older solutions

  • EE2008

  • EE2008

  • EE2008

  • EE2008

    • Reference: R. Johnsonbaugh, and R.

    M Schaefer, Chapter 5.1, , A tiling

    problem, p. 213-219. Prentice Hall

    2004.

    • Pegadory exercise: Show how this is

    done here!!

  • EE20081

  • EE2008

  • EE2008Older solutions

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    Solution for Tutor ial 11.1. Find in the following ar r ay the index of the element of 22 by applying the binary sear ch algor ithm. How many times is the binary

    search algor ithm faster than the linear search in this case?

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    4

    7

    Answer :Iteration 1:

    Low Mid High

    Iteration 2:

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    Low Mid HighIteration 3:

    Low Mid High

    Iteration 4:

    Low=Mid=High

    The index of the element is 11.

    After 4 times binary searching, the result has been found; however, 11 times may be required to find such result by using linear method. Thus, in this case,

    binary search is 3 times faster than linear method.

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

    2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 37

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    2. Write an algorithm for the recursive implementation of binary search. Answer:

    bsearch(a,i, j,key) {

    if i>j return -1; // not found k=(i+j)/2;if key=a[k] return k;if key

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    3. Draw adjacency lists for the graph

    Answer :

    1 2 3

    4

    5 6

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    4

    9

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    4. Draw adjacency lists for the digraph

    Answer :

    1 2

    34

    6 7

    5

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    0

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    5. Write an algorithm that prints the indegree and outdegree of every vertex in a digraph, where the digraph is represented using adjacency lists.

    Answer:

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    1

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    Answers of Tutorial 12

    1. List the order in which the vertices of the following graph are visited when the depth-first-search algorithm is executed with the start vertex = 4. Assume that

    the vertices are listed in increasing order in each adjacency list.

    Figure 1

    Answer:

    The order of the vertices to be visited is as follows: [From Adj[] list information]

    4, 3, 2, 1, 5, 6, 7, 8, 12, 11, 14, 10, 9, 13, 15, 16

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    2

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    2. List the order in which the vertices of the graph shown in Figure 1 are visited when the breadth-first-search algorithm is

    executed with the start vertex = 4. Assume that the vertices are listed in increasing order in each adjacency list.

    Answer:

    The order of the vertices to be visited is as follows:

    4, 3, 8, 2, 7, 12, 1, 5, 6, 11, 16, 9, 10, 15, 14, 13

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    3

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    3. Let T and T’ are two spanning trees of a connected Graph G. Suppose that an edge e is in T but not in T’. Show that there is an edge e’ is in T’ but not in

    T so that (T {e}) {e' }and (T ' {e'}) {e} are spanning trees of G.

    Proof:

    Suppose a graph G (a general case) has two spanning trees: T and T’. Since e is in T but not in T’, if we

    remove e from T, T will be broken into two connected components T1 and T2. Since all vertices in these

    two components are connected in T’, there must exist an edge e’ in T’ but not in T linking these two

    components. Thus, simply by removing e from T but add e’ we get a new spanning tree, that

    is, (T {e}) {e'}. Similarly, if we remove e’ from T’, T’ will be broken into two connected components too.

    Since all vertices in these two components are connected in T with the edge e in T, by removing e’ from

    T’ but add e we can get a spanning tree, this is (T ' {e'}) {e}.

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    4

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    T

    T’

    G

    e

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    5

    e’

    T1 T2 T1 T2

    (T {e}) {e '}T {e}

    T’1 T’2

    e’

    T ' {e '}

    T’1 T’2

    (T ' {e '}) {e}

    e

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    4. Write a stack based non-recursive algorithm implementation of the depth-first-search approach. Answer:

    dfs(adj, start){

    n=adj.last for i=1 to n

    visit[i]=false

    visit[start]=true println(start)

    s.push(start) // s is an initially empty stack while(!s.empty()){

    v=adj[s.top()] while(v!=NULL){

    if (!visit[v]){

    s.push(v)

    visit[v]=true println(v)

    break

    }

    else

    v=v.next

    } if(v==NULL)

    s.pop()

    }

    }

    ©SBH/ AY2017-18

    Page 56

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    Answers of Tutorial 13

    1. Show one topological sort of the following DAG.

    Answer : A topological sor t of this DAG is: 1, 3, 2, 6, 5, 7, 4, 8, 9, 10

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    7

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    2. Find the minimum spanning tree of the following graph by using Kruskal’s algor ithm.

    Answer: A possible order of selection of edges is:

    (2,5), (6,15), (8,12), (2,7), (10,13), (12,16), (4,8), (5,6), (5,9), (7,11), (9,10), (1,2), (15,16), (3,7), (11,14)

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    8

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    3. To maximize w(ui ,ui 1) is equivalent to minimizing logw(u ,u )

    . Since all edge weights ar e in (0,1), logw(u ,u )

    isi i 1

    1

    i i 1

    1

    positive. We can now form another graph with the same ver tices and edges but with edge weights log1

    w(u,v)for edge (u,v) . The

    problem is then the same as find a shortest path from s to d on a graph with positive weights. Dijkstra’s algorithm can be used to solve this.

    Run Dijkstra’s algorithm:

    1

    1

    4

    2

    3

    s a

    (3) b d

    (1)

    1

    1

    4

    2

    3

    s a

    b d

    (1)

    (2)(5)

    1

    1

    4

    2

    3

    s a

    b d

    (1)

    (2)(4)

    1

    1

    4

    2

    3

    s a

    (2) b

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    5

    9

    d

    (1)

    (4)

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    4. Let G be a connected weighted graph and v be a vertex in G. Suppose that the weights of the edges incident on v are distinct. Let e

    be the edge of minimum weight incident on v. Show that e is contained in every minimal spanning tree.

    Proof:

    Suppose that e=(v,w) is not contained in some spanning tree T. There is a path from v to w in T. Let

    e’=(v,w’) be the first edge on this path. Then weight(e’)>weight(e). If we delete e’ from T and add e to obtain

    a graph T’, then T’ is a spanning tree whose weight is less than T. This is a contradiction.

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    6

    0

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    5. Suppose that G (V , E) is a tree represented by an adjacency list. Write in pseudo-code an algorithm that constructs the adjacency list for a

    new graph G ' (V , E ') with the same set of vertices V as G , and with edges between any two vertices if and only if they are 2 hops away in G , i.e.,

    G' contains the edge (u, v) if and only there is a path of length 2 in G connecting u and v .

    Answer:

    two_hops_graph(adj) {

    n = adj.last

    adjnew = new array(n) for(i = 1 to n) {

    adjnew[i] = snew = null s = adj[i]

    while(s != null) {

    r = adj[s.data] while(r != null) {

    if (r.data != i) {

    temp = new node temp.data = r.data temp.next = null

    if (snew = null) adjnew[i] = temp else snew.next = temp

    snew = temp

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    6

    1

  • EE2008 AY2017-18 S2 - Tutorial 11-13 SBH

    }

    r = r.next

    }

    s = s.next

    }

    }

    return adjnew

    ©

    S

    B

    H

    /

    A

    Y

    2

    0

    1

    7

    -

    1

    8

    P

    a

    g

    e

    6

    2