Question

In: Computer Science

Write a pseudocode for the following: given that a path is a hamiltonian path, print all...

Write a pseudocode for the following: given that a path is a hamiltonian path, print all its edges.

Solutions

Expert Solution

1. Create 1D array of name path and size equal to the Number of Vertex

2. Assign -1 to all elements of array named Path

3.Let us put vertex 0 as the first vertex in the path.

    If there is a Hamiltonian Cycle, then the path can be

    started from any point of the cycle as the graph is undirected.

4. base case: If all vertices are

    included in Hamiltonian Cycle.

if (pos == V)

    {

        // And if there is an edge from the

        // last included vertex to the first vertex

        if (graph[path[pos - 1]][path[0]] == 1)

            return true;

        else

            return false;

    }

5. // Try different vertices as a next candidate

    // in Hamiltonian Cycle. We don't try for 0 as

    // we included 0 as starting point in hamCycle()

6.

for (int v = 1; v < V; v++)

    {

        /* Check if this vertex can be added

        // to Hamiltonian Cycle */

        if (isSafe(v, graph, path, pos))

        {

            path[pos] = v;

  

            /* recur to construct rest of the path */

            if (hamCycleUtil (graph, path, pos + 1) == true)

                return true;

  

            /* If adding vertex v doesn't lead to a solution,

            then remove it */

            path[pos] = -1;

        }

    }

  

    /* If no vertex can be added to

    Hamiltonian Cycle constructed so far,

    then return false */

    return false;

7. print the first vertex again to show the complete cycle

/* PROGRAM */

#include <iostream>

using namespace std;   

// Number of vertices in the graph

#define V 5

  

void printSolution(int path[]);

  

bool isSafe(int v, bool graph[V][V],

            int path[], int pos)

{

    /* Check if this vertex is an adjacent

    vertex of the previously added vertex. */

    if (graph [path[pos - 1]][ v ] == 0)

        return false;

  

    /* Check if the vertex has already been included.

    This step can be optimized by creating

    an array of size V */

    for (int i = 0; i < pos; i++)

        if (path[i] == v)

            return false;

  

    return true;

}

  

bool hamCycleUtil(bool graph[V][V],

                  int path[], int pos)

{

    /* base case: If all vertices are

    included in Hamiltonian Cycle */

    if (pos == V)

    {

        // And if there is an edge from the

        // last included vertex to the first vertex

        if (graph[path[pos - 1]][path[0]] == 1)

            return true;

        else

            return false;

    }

  

    // Try different vertices as a next candidate

    // in Hamiltonian Cycle. We don't try for 0 as

    // we included 0 as starting point in hamCycle()

    for (int v = 1; v < V; v++)

    {

        /* Check if this vertex can be added

        // to Hamiltonian Cycle */

        if (isSafe(v, graph, path, pos))

        {

            path[pos] = v;

  

            /* recur to construct rest of the path */

            if (hamCycleUtil (graph, path, pos + 1) == true)

                return true;

  

            /* If adding vertex v doesn't lead to a solution,

            then remove it */

            path[pos] = -1;

        }

    }

  

    /* If no vertex can be added to

    Hamiltonian Cycle constructed so far,

    then return false */

    return false;

}

  

bool hamCycle(bool graph[V][V])

{

    int *path = new int[V];

    for (int i = 0; i < V; i++)

        path[i] = -1;

      path[0] = 0;

    if (hamCycleUtil(graph, path, 1) == false )

    {

        cout << "\nSolution does not exist";

        return false;

    }

  

    printSolution(path);

    return true;

}

  

/* A utility function to print solution */

void printSolution(int path[])

{

    cout << "Solution Exists:"

            " Following is one Hamiltonian Cycle \n";

    for (int i = 0; i < V; i++)

        cout << path[i] << " ";

  

    // Let us print the first vertex again

    // to show the complete cycle

    cout << path[0] << " ";

    cout << endl;

}

  

// Driver Code

int main()

{

  bool graph1[V][V] = {{0, 1, 0, 1, 0},

                        {1, 0, 1, 1, 1},

                        {0, 1, 0, 0, 1},

                        {1, 1, 0, 0, 1},

                        {0, 1, 1, 1, 0}};

      

    // Print the solution

    hamCycle(graph1);

      

    bool graph2[V][V] = {{0, 1, 0, 1, 0},

                         {1, 0, 1, 1, 1},

                         {0, 1, 0, 0, 1},

                         {1, 1, 0, 0, 0},

                         {0, 1, 1, 0, 0}};

  

    // Print the solution

    hamCycle(graph2);

  

    return 0;

}

/* OUTPUT */

Solution Exists: Following is one Hamiltonian Cycle
 0  1  2  4  3  0

Solution does not exist

/* PLEASE UPVOTE */


Related Solutions

write the pseudocode and code to solve the following: you are given a list. determine the...
write the pseudocode and code to solve the following: you are given a list. determine the sum of all the elements in the list without using the built in puthon function sum(). Take your code and create your own function and name it my_sum_algo() which will return the sum of numbers PS please write comments in the code for my understanding, and please write jn jn PYTHON 3 languge. Thank you, have a great day !
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
Write a C++ program to print all the perfect numbers below a certain given number. A...
Write a C++ program to print all the perfect numbers below a certain given number. A perfect number is a number that that is equal too the sum of it's divisors other than itself . For example, 6 and 28 are all perfect numbers. 6 = ( 1+2 +3) 28 = (1+2+4+7+14) Make sure your program conforms to the following requirements: 1. Accept the upper limit from the user (as an integer). 2. Make sure the number is positive (at...
Write a C++ function to print out all unique letters of a given string. You are...
Write a C++ function to print out all unique letters of a given string. You are free to use any C++ standard library functions and STL data structures and algorithms and your math knowledge here. Extend your function to check whether a given word is an English pangram (Links to an external site.). You may consider your test cases only consist with English alphabetical characters and the character.
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
Write out the full Hamiltonian for the Be atom.
Write out the full Hamiltonian for the Be atom.
What is the relation between path integral methods for dealing with constraints (constrained Hamiltonian dynamics involving...
What is the relation between path integral methods for dealing with constraints (constrained Hamiltonian dynamics involving non-singular Lagrangian) and Dirac's method of dealing with such systems (which involves the Dirac bracket)? And what is the advantage/relative significance of each method?
Write the Pseudocode for the following programming problem. Write a program that will calculate a tip...
Write the Pseudocode for the following programming problem. Write a program that will calculate a tip based on the meal price and a 6% tax on a meal price. The user will enter the meal price and the program will calculate tip, tax, and the total. The total is the meal price plus the tip plus the tax. Your program will then display the values of tip, tax, and total. The tip amounts based on the mean price are as...
Write a C++ function to print any given std::array of a given type to the standard...
Write a C++ function to print any given std::array of a given type to the standard output in the form of {element 0, element 1, element 2, ...}. For example given the double array, d_array = {2.1, 3.4, 5.6, 2.9}, you'd print {2.1, 3.4, 5.6, 2.9} to the output upon executing std::cout << d_array; line of code. Your function mus overload << operator.
Write a JAVA program that contains the following 4 void methods whic will print all subtrings...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings of s in specific orders: printSub1(String s), printSub2(String s), printSub3(String s), and printSub4(String s) For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "a", "ab", "b", "abc", "bc", "c", "abcd", "bcd", "cd", "d" printSub3 will print "d", "c", "b", "a", "cd", "bc", "ab", "bcd", "abc", "abcd" printSub4 will print "abcd", "bcd",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT