Question

Please show me the pseudo code. This pseudo code is used for detect whether a given...

Please show me the pseudo code.

This pseudo code is used for detect whether a given undirected graph contains a cycle: a sequence of nodes that starts and ends in the same node without traversing the same edge twice.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We use the find-union algorithm to detect a cycle in an undirected graph.

We initially set the parent array to -1 for all nodes.

And array indexing starts from 1, so array is 1,2......,n

find(parent[],i) //function to find root of node i using the parent array

{

if(parent[i]==-1) //if we reach a node which does not have a parent

return i; //we return the node as the root

else

return find(parent,parent[i]); //else we find the root of the parent of the node

}

union(parent[],x,y) //function to join two nodes x and y with different parents

{

xParent=find(parent,x); //finding root of x

yParent=find(parent,y); //finding root of y

if(xParent!=yParent) //if they both do not have same root

parent[xParent]=yParent; //we set root of root of x to be root of y

}

findCycle(a[][],n) //function to return if Graph has a cycle, a[][] is the adjacency matrix and n is the number of nodes in the graph

{

parent[]= array of size n with all elements set to -1;

for i = 1 to n-1

{

for j = i to n

{

if(a[i][j]==1) //if there is an edge between nodes i and j

{

//we find the roots of i and j

x=find(parent,i);

y=find(parent,j);

if (x==y) //if they have same roots, a cycle is formed

return true;

else

union(parent,x,y);

}

}

}

return false; //if program reaches this point, no cycle was detected

}

Add a comment
Know the answer?
Add Answer to:
Please show me the pseudo code. This pseudo code is used for detect whether a given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Java if possible please: Write an algorithm using pseudo code to determine if an undirected...

    Use Java if possible please: Write an algorithm using pseudo code to determine if an undirected graph has any cycle. Analyze the complexity of your algorithm. Write an algorithm using pseudo code to determine if an undirected graph is connected or not. Analyze the complexity of your algorithm. (i) (ii)

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • Problem 3: Suppose you are given an undirected graph G and a specified starting node s...

    Problem 3: Suppose you are given an undirected graph G and a specified starting node s and ending node t. The HaMILTONIAN PATH problem asks whether G contains a path beginning at s and ending at t that touches every node exactly once. The HAMILTONIAN CYCLE problem asks whether con- tains a cycle that touches every node exactly once (cycles don't have starting or ending points, so s and t are not used here) Assume that HaMIlTonian CYCLe is NP-Complete....

  • please answer one of the two 1. (25) [Single-source shortest-path: algorithm tracing] Show the tracing of...

    please answer one of the two 1. (25) [Single-source shortest-path: algorithm tracing] Show the tracing of Dijkstra's shortest path search algorithm on the weighted directed graph shown below. Do the tracing it twice, first starting with the nodes and, second, starting with the node z. For each tracing, each time the shortest path to a new node is determined, show the graph with the shortest path to the node clearly marked and show inside the node the shortest distance to...

  • Question 1.) Given a directed negative weights Graph what is the most efficient algorithm to detect...

    Question 1.) Given a directed negative weights Graph what is the most efficient algorithm to detect a cycle. What is the most efficient method to detect a cycle? Question 2.)Let A= student id last digit % 4 and add 1. For example mine student id is 7. So 7%4=3 and add 1 -->A=4 Run DFS starting at node a, and breaking ties alphabetically(in this case numerically as nodes have numbers where 1 goes before 2). Please label just back edges....

  • I need this in C programing And please give me complete code with screenshot of your...

    I need this in C programing And please give me complete code with screenshot of your output. onded reverze. e le defnes α node stricture identical to the list nodes for the cycle detection probem in Lab4 3. Cycle le removal (20 points). Implemeat the function break.cycleO in The prototype of the fnction s The function receives as its sole argment a poister to the the link that closwes the cycle, list. If the list contains a cycle the function...

  • Can you tell me how to solve it? 2 Search Problem 1 Misc 1. Consider the...

    Can you tell me how to solve it? 2 Search Problem 1 Misc 1. Consider the two graphs shown in Figures 1 and 2. In what order does BTS, DFS and UCS (assume you use the graph scarch version) cxpand the nodes for cach graph (assume that nodes are added to the stack/queue in alphabetical order)? The agent starts at nodes and must reach node g. (Note: All iterations might not be necessary.) (Note: The CurrNode in the table represents...

  • How would I traverse through this graph? Provide example code, please! class Edge {    int src,...

    How would I traverse through this graph? Provide example code, please! class Edge {    int src, dest;    Edge(int src, int dest)    {        this.src = src;        this.dest = dest;    } }; // class to represent a graph object class Graph {    // A list of lists to represent adjacency list    List<List<Integer>> adj = new ArrayList<>();    // Constructor to construct graph    public Graph(List<Edge> edges)    {        // allocate memory for adjacency list        for (int i = 0; i < edges.size(); i++) {            adj.add(i,...

  • Given the following pseudo-code, please show what the outputs will be at LINE A, B, C...

    Given the following pseudo-code, please show what the outputs will be at LINE A, B, C and D. please put down “inconclusive” if you think the CPU process scheduling could potentially result in multiple values for integer i. You will receive partial credit if you draw the process tree. int i = 0; int main(){                         if (fork() == 0) {                         i++;                         if (fork() == 0){                                     if (fork() == 0){                                                 i++;                                                 print value...

  • 1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing...

    1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing the nodes added to the min spanning tree and the order they are added. Using Python. Starter code: from collections import deque class Edge: def __init__(self, endIndex, next = None): self.endIndex = endIndex self.next = next class Node: def __init__(self, name): self.name = name self.visited = False self.connects = None class Graph: def __init__(self): self.nodeList = [] self.size = 20...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT