answersLogoWhite

0


Best Answer

As a rough outline, we start with some vertex x, and build a list of the vertices you can get to from x. Each time we find a new vertex to be added to this list, we check its neighbors to see if they should be added as well. Finally, we check whether the list covers the whole graph. In pseudocode: test-connected(G)

{

choose a vertex x

make a list L of vertices reachable from x,

and another list K of vertices to be explored.

initially, L = K = x.

while K is nonempty

find and remove some vertex y in K

for each edge (y,z)

if (z is not in L)

add z to both L and K

if L has fewer than n items

return disconnected

else return connected

}

To analyze the algorithm, first notice that the outer loop happens n times (once per vertex). The time for the inner loop (finding all unreached neighbors) is more complicated, and depends on the graph representation. One key step (testing whether z is in L) seems like it might be slow, but can be done quickly by keeping a bit on each vertex that says whether it's in L or not. * For the object oriented representation, each execution of the inner loop involves scanning through all m edges of the graph. So the total time for the algorithm is O(mn). * For the adjacency matrix representation, each execution of the inner loop involves looking at a single row of the matrix, in time O(n). So the total time for the algorithm is O(n^2). * In the adjacency list (or incidence list) representation, each element on each list is scanned through once. So the total time on all executions of the inner loop is the same as the total length of all adjacency lists, which is 2m. Note that we don't multiply this by n, even though this is a nested loop -- we just add up the number of times each statement is executed in the overall algorithm. The total time for the algorithm is O(m+n) At the end of the algorithm, the list L tells you one connected component of the graph (how much of the graph can be reached from x). With some more care, we can find all components of G rather than just one component. If graph is connected, we can modify the algorithm to find a tree in G covering all vertices (a spanning tree): For each z, let parent(z) be the vertex y corresponding to the time at which we added z to L. This gives a graph in which each vertex except x is connected to some previous vertex, and any such graph must be a tree

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

We'll need to keep track of two things:

1) The vertices we've already visited.

2) The vertices we need to visit.

Algorithm in pseudocode:

Stack toVisit

Set visited

add any vertex to toVisit

while toVisit is not empty

currentVertex = toVisit.pop()

visited.add(currentVertex)

for each vertex in neighbors of currentVertex

if vertex is not in visited

toVisit.push(vertex)

If visited contains all vertices in the graph, graph is connected; else, graph is not connected.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

graphIsConnected = true

for (current = ) each vertex in graph

for (other = ) each other vertex in graph

if not (current isConnectedTo other)

graphIsConnected = false

end

if graphIsConnected

// yay!

If you want an implementation for isConnectedTo, I suggest using a version of a depth-first search algorithm modified to check for loops.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Design an algorithm to check if a given graph is connected?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How can you design an algorithm to check if a given graph is connected?

Use a simple DFS/BFS traversal. If you have gone through all nodes, the graph is connected.


Design an algorithm for finding integer solutions for equations of the form x2 y2 n where n is some given positive integer Determine the time complexity of your algorithm?

yea me too dude. Mahleko :(


Write an algorithm to check whether the given number is odd or even?

Type your answer here... i think we should first enter 1 number then check it


What is complsexity of an algorithm?

Complexity of an algorithm is a measure of how long an algorithm would take to complete given


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


What is devising of algorithms?

The algorithm is designed through algorithm engineering. The Algorithm design refers to one of the specific methods that is used in creating the mathematical process that is used in problem solving.


What has the author Charles H C Little written?

Charles H. C. Little has written: 'An algorithm for finding a canonical surface imbedding for a given planar connected graph'


Diagram used to represent an algorithm or sequence of actions in the form of boxes connected with arrows?

this is possibly a FLOW CHART given to me on another crossword puzzle site!! lamu lady


How do you use prim's algorithm to find a spanning tree of a connected graph with no weight on its edges?

Prims Algorithm is used when the given graph is dense , whereas Kruskals is used when the given is sparse,we consider this because of their time complexities even though both of them perform the same function of finding minimum spanning tree. ismailahmed syed


Why algorithm needs to solve programming problem?

This is the definition of an algorithm - a list of orders of how to solve a given programming problem.


You are given a list of students names and their tests scores Design an algorithm that does the following calculates the average test scores?

(x)/(y)=avg X= Total of Scores. Y= Total of Students.


What is Vector generation algorithm?

It is a basic algorithm for generating lines on computer screen. line is generated between given 2 endpoints