algo-lab-2

Upload: dipanjan-bhattacharya

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Algo-Lab-2

    1/48

    Design & Analysis of Algorithms

    Laboratory Instructions & Assignments

    By: Tamal Chakraborty

  • 7/29/2019 Algo-Lab-2

    2/48

    WEEK 6

    Minimum Spanning Tree Algorithms

  • 7/29/2019 Algo-Lab-2

    3/48

    3

    Minimum Spanning Tree A tree T is said to be a spanning tree of a connected graph G if T is a sub-

    graph of G and T contains all vertices of G.

    The weight of a spanning tree T of G is defined as the sum of the weightsof all the branches of T.

    A spanning tree with the smallest weight in a weighted graph is called aminimal spanning tree.

    Given a weighted graph G, we have to find a minimal spanning tree of thegraph.

    v4

    v3

    v2

    v1

    5

    3

    2

    4

    1

  • 7/29/2019 Algo-Lab-2

    4/48

    4Finding The Minimal SpanningTree

    Let A be a sub-set of the minimal spanning tree

    At each step determine an edge (u, v) such that, if we add (u,v) to A, A remains a subset of the minimal spanning tree.

    GreedyStrategy

    The edge (u, v) iscalled a safe edge

    for A.

  • 7/29/2019 Algo-Lab-2

    5/48

    5

    Kruskals Algorithm

    1. List all the edges of the graph G in order of non-decreasing weight.

    (v2, v3), (v3, v4), (v1, v3), (v1, v2), (v2, v4)

    2. Select a smallest edge from G

    (v2, v3)

    3. From the remaining edges select the smallest edge which doesnt make a circuitwith the previously selected edges.

    (v2, v3), (v3, v4)

    4. Continue until (n1) edges have been selected

    (v2, v3), (v3, v4), (v1, v3)

    v3

    v1

    v2v4

    7

    5

    8

    2

    3

    v3

    v1

    v2v4

    5

    2

    3

  • 7/29/2019 Algo-Lab-2

    6/48

    6

    Strategy Read the input graph from a file using the readGraph method (refer to

    Assignment 3.1)

    Get an array of edges of the graph using the getEdges method (refer toAssignment 3.2)

    Sort the edges of the graph using the qsort method (refer to Assignment5.2)

    The function to compare edges may be implemented as:int edgeCmp(const void* a1, const void* a2) {

    edge* e1 = (edge*)a1;

    edge* e2 = (edge*)a2;

    return (e1->cost - e2->cost);

    } To check whether a circuit is formed with the previously selected edges w

    will use the compressedFind and weightedUnion operation of Disjoint Se(refer to Assignment 5.1)

  • 7/29/2019 Algo-Lab-2

    7/48

    7

    Assignments

    6.1 Implement Kruskals Algorithm:

    void kruskal(G)

    initDisjointSet(n);

    k = getEdges(G, edges);

    qsort(edges, k, sizeof(edge), edgeCmp)

    for (i = 0 to k - 1)

    u = edges[i].u

    v = edges[i].v

    if (compressedFind(u) compressedFind(v))

    weightedUnion(u, v)print edge (u, v) in MST

    if (++nodes == n - 1) break

  • 7/29/2019 Algo-Lab-2

    8/48

    8

    Prims Algorithm

    v3

    v1

    v2v4

    7

    5

    8

    2

    3

    038

    3025

    8207

    570

    1. Start with vertex v1 and connect to the vertex which has the smallest entry in row1, say vk.

    (v1, v3)2. Now consider (v1, vk) as one sub-graph and connect this sub-graph to a vertex

    other than v1 and vkthat has the smallest entry among all entries in rows 1 and k,say vj.

    (v1, v3), (v3, v2)

    3. Now regard the tree with vertices v1, vkand vj as one sub-graph and continuethis process until n vertices have been connected by n 1 edges.

    (v1, v3), (v3, v2), (v3, v4)

    v3

    v1

    v2v4

    5

    2

    3

  • 7/29/2019 Algo-Lab-2

    9/48

    9

    Strategy Read the input graph from a file using the readGraph method (refer to Assignm

    3.1). Pass the weighted adjacency matrix G and a source vertex s to the Pr

    function. Initialize an array d[] of size n using the init method, set d[s] to 0 and all oth

    elements of d[] to . Keep the init method in a separate file Common.h

    init(G, s)

    foreach (vertex v of G)

    d[v] =

    p[v] = -1d[s] = 0

    Initialize a priority queue with the d[] array (insert each element of d in the priorqueue)

    At every step get a vertex u, using the extractMin method of the priority queue

    For each vertex v adjacent to u, if v exists in the priority queue, let i be its indexthe heap, then set p[v] to u, d[v] to G[u][v] and decrease the key of the ith elementpriority queue to d[v]

    For each element i of the array p[1 n - 1], the pair (p[i], i) gives an edge in the M

  • 7/29/2019 Algo-Lab-2

    10/48

    1

    Assignments

    6.2 Implement Prims Algorithm:

    void prim(G, s)

    init(G, s);

    PriorityQueue* pq = initPQ(d, n);

    while ((e = extractMin(pq)))

    u = eindexfor (v = 0 to n - 1)

    if ((G[u][v]) AND (G[u][v] ))

    i = findPQ(pq, v)

    if (i AND G[u][v] < d[v])

    p[v] = u

    d[v] = G[u][v]

    decreaseKey(pq, i, d[v])

  • 7/29/2019 Algo-Lab-2

    11/48

    WEEK 7

    Knapsack & MaxMin Algorithms

  • 7/29/2019 Algo-Lab-2

    12/48

    1

    Knapsack Problem A Thief enters a store in the night, with a knapsack in his hand.

    The knapsack can carry only a certain amount of weight, say W at most. The store has n items, each having a value V and weight W.

    The thief has to select items, such that he fills his knapsack with itemgiving him the maximum value.

    There are two variations of this problem.

    In the 0/1 knapsack problem the thief has to either take the entire itemleave it altogether.

    In the fraction knapsack problem the thief is allowed to take fractionitems.

    For example, if the store contained gold biscuits it would constitute a 0/knapsack problem. Whereas if the store contained gold dust, it wouconstitute a fraction knapsack problem, since the thief can take a portionthe gold dust.

  • 7/29/2019 Algo-Lab-2

    13/48

    1

    Example

    Let us suppose the store has three items as shown below:

    Let the Knapsack capacity be 50.

    The thief would like to make a greedy choice, hence he will pick upitems with higher V/W values.

    Item Value (V) Weight (W) V/W

    1 60 10 6

    2 100 20 5

    3 120 30 4

  • 7/29/2019 Algo-Lab-2

    14/48

    1

    Example: 0/1 Knapsack Problem

    Let us assume that it is a 0/1 Knapsack Problem, i.e. the thief has toeither take the whole item or leave it.

    By making a greedy choice the thief would pick up items 1 & 2 first,thereby filling the knapsack by W = 30 and earning V = 160

    He cant take the item 3 anymore since his knapsack can only take 20

    units of weight, whereas item 3 weighs 30. But as we can see, if the thief picked up items 2 & 3 he would have

    earned V = 220.

    Thus greedy choice did not lead to an optimal solution

    1l k

  • 7/29/2019 Algo-Lab-2

    15/48

    1Example: Fraction KnapsackProblem

    Let us assume that it is a Fraction Knapsack Problem, i.e. the thief cantake a part of an item.

    By making a greedy choice the thief would pick up items 1 & 2 first,thereby filling the knapsack by W = 30 and earning V = 160

    He will now take 20 units of weight of item 3, thereby gaining another

    80 units of money. His total profit is 160 + 80 = 240

    Thus greedy choice leads to an optimal solution for a fractionknapsack problem.

    1

  • 7/29/2019 Algo-Lab-2

    16/48

    1

    Strategy Implement each item as a structure.

    typedef struct item

    { int weight;int value;

    }item;

    Sort the items in decreasing order of value/weight by the qsort method. The itcomparator method may be implemented as:

    int compare(const void *x, const void *y){

    item *i1 = (item *)x, *i2 = (item *)y;

    double ratio1 = (*i1).value*1.0 / (*i1).weight;

    double ratio2 = (*i2).value*1.0 / (*i2).weight;

    return ratio2 ratio1;

    } Finally iterate over items and select them by greedy choice.

    1

  • 7/29/2019 Algo-Lab-2

    17/48

    1

    Assignment

    7.1 Implement the Knapsack algorithm

    Knapsack(item items[], int n, int maxWeight)qsort(items, n, sizeof(item), compare)value = 0.0

    presentWeight = 0for(i = 0 to n 1)

    if (presentWeight + items[i].weight < maxWeight)

    presentWeight += items[i].weightvalue += items[i].value

    elseremaining = maxWeight - presentWeightvalue += items[i].value * remaining * 1.0 / items[i].weightbreak

    return value

    1

  • 7/29/2019 Algo-Lab-2

    18/48

    1

    MaxMin Algorithm

    Let us consider the problem of finding the maximum and minimum o

    an array a[1 n] of n integers. We would formulate a divide and conquer algorithm to solve this as

    follows:

    If the array contains one element then max and min are both the same(which is the only element in the array)

    If the array contains two elements the greater of them is max and thelesser of them is min

    If the array contains more than two elements divide it into two parts PP2 along the mid-point, and then find the max and min of the two par

    recursively. Then max = larger(max(P1), max(P2) and min =smaller(min(P1), min(P2))

    1

  • 7/29/2019 Algo-Lab-2

    19/48

    1

    Assignment

    7.2 Implement the MaxMin Algorithm

    maxMin(i, j, max, min)if (i == j) max = min = a[i]else if (i == j 1)

    if (a[i} > a[j])max = a[i]

    min = a[j]else

    max = a[j]min = a[i]

    elsemid = (i + j) / 2maxMin(i, mid, max, min)maxMin(mid + 1, j, max1, min1)if (max < max1) max = max1if (min > min1) min = min1

  • 7/29/2019 Algo-Lab-2

    20/48

    WEEK 8

    Single Source Shortest Path

    2

  • 7/29/2019 Algo-Lab-2

    21/48

    2

    Shortest Path Problem

    A simple weighted directed graph G can be represented by an n x nmatrix D = [dij] where:

    dij = weight of the directed edge from i o j

    = 0, if i = j

    = , if there is no edge between i and j We have to find out the shortest path from any given vertex to all other

    vertices

    v3

    v1

    v2v4

    7

    5

    8

    2

    3

    0

    30

    820

    570

    D

    2

  • 7/29/2019 Algo-Lab-2

    22/48

    2

    Dijkstras Algorithm

    Begin1. Assign a permanent label 0 to the start vertex and a temporary label

    to all other vertices

    2. Update label of each vertex j with temporary label using the followingrule:

    Labelj = min[Labelj, Labeli + dij]Where i is the latest vertex permanently labeled and dij is the directdistance between i and j.

    3. Choose the smallest value among all the temporary labels as the newpermanent label. In case of a tie select any one of the candidates.

    4. Repeat steps 2 and 3 until all the vertices are permanently labeled

    End

    2

  • 7/29/2019 Algo-Lab-2

    23/48

    Dijkstras Algorithm

    v1 v2 v3 v4

    0

    0 7 5

    0 7 5 8

    0 7 5 8

    v3

    v1

    v2v4

    7

    5

    8

    2

    3

    2

  • 7/29/2019 Algo-Lab-2

    24/48

    Strategy

    Read the input graph from a file using the readGraph method (refer to

    Assignment 3.1). Pass the weighted adjacency matrix G and a source verts to the dijkstra function.

    Initialize an array d[] of size n using the init method, set d[s] to 0 and allother elements of d[] to . Refer to Assignment 6.2

    Initialize a priority queue with the d[] array (insert each element of d in t

    priority queue) At every step get a vertex u, using the extractMin method of the prior

    queue

    For each vertex v adjacent to u, call relax(u, v, G) and decrease the key oin the priority queue to d[v]

    The relax method can be implemented as follows (put it in Common.h)relax(u, v, G)

    if (d[u] > d[v] + G[u][v])d[u] = d[v] + G[u][v]

    p[v] = u

    2

  • 7/29/2019 Algo-Lab-2

    25/48

    Assignment

    8.1 Implement Dijkstras Algorithm

    dijkstra(int G[][n], int s)init(G, s)PriorityQueue* pq = initPQ(d, n)while ((e = extractMin(pq)))

    u = e->indexfor (v = 0 to n - 1)if ((G[u][v]) AND (G[u][v] ))

    relax(u, v, G)decreaseKey(pq, findPQ(pq, v), d[v])

    2

  • 7/29/2019 Algo-Lab-2

    26/48

    Bellman-Ford Algorithm

    The Bellman-Ford algorithm solves the single source shortest path

    problem for graphs, in which the edge weights may be negative The Bellman-Ford algorithm indicates whether or not there is a

    negative weight cycle, reachable from the source, in the graph

    If there is such a cycle the algorithm indicates that no such solutionexists by returning a Boolean false

    If there is however no such cycle the algorithm produces the shortestpaths from the source vertex to every other vertex and their weights

    2

  • 7/29/2019 Algo-Lab-2

    27/48

    Assignment

    8.2Implement Bellman-FordAlgorithm

    bellmanFord(int G[][n], int s)init(G, s)

    for (i = 1 to n - 1)for (u = 0 to n - 1)

    for (v = 0 to n - 1)

    if ((G[u][v]) AND (G[u][v] ))relax(u, v, G)

    for (u = 0 to n - 1)for (v = 0 to n - 1)

    if ((G[u][v]) AND (G[u][v] ))if (d[v] > d[u] + G[u][v]) return 0

    return 1

  • 7/29/2019 Algo-Lab-2

    28/48

    WEEK 9

    All Pairs of Shortest Paths, N-Queens

    2

  • 7/29/2019 Algo-Lab-2

    29/48

    All pairs of shortest paths

    Given a vertex of a graph, Dijkstras algorithm enables us to find theshortest path from that vertex to all other vertices

    The next problem is to find out the shortest path between any givenpair of vertices of a graph

    The restriction is that G have no cycles with negative length

    If we allow G to contain cycles with negative length then the shortest

    path between any two vertices on this cycle is - The all pairs of shortest path problem is to determine a matrix A such

    that A(i, j) is the length of the shortest path from i to j.

    3

  • 7/29/2019 Algo-Lab-2

    30/48

    All pairs of shortest paths We assume all the vertices of the graph are numbered from 1 to n

    Let Ak

    (i, j) be the length of the shortest path from i to j going through nointermediate vertex greater than k

    Then there are two possibilities

    1. The path from i to j goes through k: In which case we can split the patin two parts, one from i to k and the other from k to j. Note that neithe

    of these two paths can go through any intermediate vertex greater thak 1. Length of such a path is: Ak-1(i, k) + Ak-1(k, j)

    2. The path from i to j does not go through k: Which means that this patgoes through no intermediate vertex greater than k-1. Its length woulbe: Ak-1(i, j)

    Clearly A

    k

    (i, j) is the minimum of these two choices Hence Ak(i, j) = min{ Ak-1(i, j) , Ak-1(i, k) + Ak-1(k, j) }

    3

    i

  • 7/29/2019 Algo-Lab-2

    31/48

    Assignment

    9.1 Implement the all pairs of shortest paths algorithm

    allPairs(int a[][n])for (k = 0 to n - 1)

    for (i = 0 to n - 1)for (j = 0 to n - 1)

    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);

    3

  • 7/29/2019 Algo-Lab-2

    32/48

    The 8 Queens Problem

    We have an 8x8 chessboard, ourjob is to place eight queens on thechessboard, so that no two ofthem can attack. That is, no two ofthem are in the same row, columnor diagonal

    QQ

    Q

    Q

    Q

    Q

    Q

    Q

    3

  • 7/29/2019 Algo-Lab-2

    33/48

    The n Queens Problem

    Q1

    The generalized version of the 8 Queens problem is the n Queensproblem, where n is a positive integer. Let us illustrate abacktracking approach to this problem with a 4 Queens example

    Step1: Place Q1 on Col 1

    Q1

    X X Q2

    Step2: Place Q2

    Q1

    X X Q2

    X X X X

    No legal place for Q3

    3

  • 7/29/2019 Algo-Lab-2

    34/48

    The n Queens Problem

    Q1

    X X X Q2

    Backtrack and alter Q2

    Q1

    X X X Q2

    X Q3

    Place Q3

    Q1

    X X X Q2

    X Q3

    X X X X

    No legal place for Q4

    Q1

    Backtrack and alter Q1

    Q1

    X X X Q2

    Place Q2

    Q1

    X X X Q2

    Q3

    X X Q4

    Place Q3 and then Q4

    3

  • 7/29/2019 Algo-Lab-2

    35/48

    The n Queens Problem

    Let (x1, x2, , xn) represent a solution inwhich xi is the column of the i

    th row,where the ith queen has been placed.The xis will all be distinct, since no twoqueens can be in the same column.Now, how to check that they are not in

    the same diagonal?

    QQ

    Q

    Q

    Q

    Q

    Q

    Q

    3

    Th bl

  • 7/29/2019 Algo-Lab-2

    36/48

    The n Queens Problem

    Let the chessboard square be representedby the 2D array a[18, 18].Every element on the same diagonal thatruns from top-left to right-bottom has thesame (row column) value.For example, a Queen at a[4,2] hasattacking Queens, diagonal to it at a[3,1],

    a[5,3], a[6,4] (up-left to lowright).

    QQ

    Q

    Q

    Q

    Q

    Q

    Q

    3

    Th Q P bl

  • 7/29/2019 Algo-Lab-2

    37/48

    The n Queens Problem

    Also all elements on the same diagonalfrom top-left to bottom-right have thesame (row + column) value.If the Queens are placed at (i,j) and (k,l)cells, then they are in the same diagonalonly if

    i j = k l ori + j = k + li.e.abs(j l) = abs(i k)

    QQ

    Q

    Q

    Q

    Q

    Q

    Q

    3

    Assignment

  • 7/29/2019 Algo-Lab-2

    38/48

    Assignment

    9.2 Implement the n-queens algorithm

    place(k, i)for (j = 1 to k - 1)

    if ((x[j] == i) || (abs(x[j] - i) == abs(j - k)))return 0;

    return 1;

    nQueens(k)for (i = 1 to n)

    if (place(k, i))x[k] = i;if (k == n) displayChessBoard();else nQueens(k + 1);

  • 7/29/2019 Algo-Lab-2

    39/48

    WEEK 10

    Graph Coloring, Hamiltonian Cycles

    4

    Graph Coloring Problem

  • 7/29/2019 Algo-Lab-2

    40/48

    Painting all the vertices of a graph with colors such that no twoadjacent vertices have the same color is called the proper coloring of agraph.

    A graph in which every vertex has been assigned a color according toproper coloring is called a properly colored graph.

    A graph G that requires k different colors for its proper coloring, andno less, is called a k-chromatic graph. The number k is called thechromatic number of G.

    Graph Coloring Problem

    4

    Graph Coloring Problem

  • 7/29/2019 Algo-Lab-2

    41/48

    Let m be a given positive integer. In our example, say m = 3. We want to find whether the nodes of G can be colored in such a way

    that no two adjacent nodes have the same color, yet only m colors areused.

    We design a backtracking algorithm such that given the adjacency

    matrix of a graph G and a positive integer n, we can find all possibleways to properly color the graph.

    Graph Coloring Problem

    4

    Graph Coloring Problem: How it works

  • 7/29/2019 Algo-Lab-2

    42/48

    Let Red be color 1

    Let Green be color 2

    Let Blue be color 3

    Let us examine how the backtracking algorithm for coloringworks:

    Graph Coloring Problem: How it works

    ab

    e

    dc

    4

    Graph Coloring Problem

  • 7/29/2019 Algo-Lab-2

    43/48

    Graph Coloring Problem

    a be

    dc

    a be

    dc

    ab

    e

    dc

    ab

    e

    dc

    4

    Assignment

  • 7/29/2019 Algo-Lab-2

    44/48

    Assignment

    10.1 Implement the Graph Coloring Algorithm

    void nextColor(k, G)do {

    x[k] = (x[k] + 1) % (m + 1);if (x[k] == 0) return;int j;

    for (j = 0; j < n; j++)if ((G[j][k] 0) AND (x[k] == x[j])) break;

    if (j == n) return;} while (true);

    void mColoring(k, G)do {

    nextColor(k, G);if (x[k] == 0) return;if (k == n - 1) display();

    else mColoring(k + 1, G);} while (true);

    4

    Hamiltonian Cycles

  • 7/29/2019 Algo-Lab-2

    45/48

    Let G(V, E) be a connected graph with n vertices. A Hamiltonian cycle is a roundtrip path in G that visits every vertex once and returns to the starting position.

    The graph G1 below has Hamiltonian cycles.

    Whereas G2 has no Hamiltonian cycles.

    We want to find all the Hamiltonian cycles in a graph

    Hamiltonian Cycles

    7

    0 1 2

    56

    3

    4

    01

    2

    34

    4

    Hamiltonian Cycles

  • 7/29/2019 Algo-Lab-2

    46/48

    The following figure illustrates the backtracking approach for apossible solution

    Hamiltonian Cycles

    a

    b

    c

    d

    e

    f

    e

    d f

    f

    e

    c

    d

    aDead end

    Dead end Dead end

    Solution

    a b

    f

    ed

    c

    Given graph

    Part of the solution tree

    4

    Assignment

  • 7/29/2019 Algo-Lab-2

    47/48

    Assignment

    10.2 Implement the Hamiltonian Cycles Algorithm

    nextVertex(k, G)do {

    x[k] = (x[k] + 1) % (n + 1);if (x[k] == 0) return;if (G[x[k - 1] - 1][x[k] - 1] 0) {

    for (j = 1 to k - 1)if (x[k] == x[j]) break;

    if (j == k)if ((k < n) OR ((k == n) AND G[x[n] - 1][x[1] - 1]))

    return;} while (true);

    hamilton(k, G)do {

    nextVertex(k, G);

    if (x[k] == 0) return;if (k == n) displayCycle();else hamilton(k + 1, G);

    } while (true);

  • 7/29/2019 Algo-Lab-2

    48/48

    Thank you!!!

    If debugging is the process of removing bugs, then programmingmust be the process of putting them in.

    - Dijkstra