Graph reachability Ques
3 posters
Page 1 of 1
Graph reachability Ques
Let the graph vertices are numbered from 1 to |V|.....let
min(i)=min number of the vertex reachable from vertex number i
example- let from vertex 2 i can reach to vertex number 1,5,6....so min(2)=1....
give an algorithm to find min(i) for all the vertices of the graph....
Achi time complexity mei nikalna....
min(i)=min number of the vertex reachable from vertex number i
example- let from vertex 2 i can reach to vertex number 1,5,6....so min(2)=1....
give an algorithm to find min(i) for all the vertices of the graph....
Achi time complexity mei nikalna....
shivang- Posts : 42
Join date : 2009-01-30
Age : 35
Re: Graph reachability Ques
Can b solved in O(n) time.
------------------------------
------------------------------
- Code:
Take a MinArray[n] to store the Min Result.
Initialize MinArray[i=(0 to n-1)] to some MAX value;
for( i in V[Graph] )
{
call mod-DFS(i);
}
print Array[i=(0 to n-1)]
- Code:
int mod-DFS( int i )
{
if( color[i]==0 ) //checks if we're not done with i'th vertex
{
color[i] = 1;
for( every possible vertex j from i )
{
if(color[i]==1)
MinArray[i] = minimum( MinArray[i], j );
else
MinArray[i] = minimum( MinArray[i], j, mod-DFS(j) );
}
color[i] = 2;
}
return MinArray[i];
}
Last edited by Dee2306 on Sat Feb 07, 2009 4:49 pm; edited 2 times in total (Reason for editing : Sorry , the Last Solution Could go Into an Infinite Loop !!)
Dee2306- Posts : 3
Join date : 2009-01-31
Re: Graph reachability Ques
Here I assume that, if I can go from i'th node to j'th node,
such that ( i < j ) is true, So the answer should not be 'i' but 'j'.
i.e. ( MinArray[i] = j ) in this case.
the other way, i.e. if I want to have ( MinArray[i] = i ) in the above case
I could have modified some of the lines as ..
such that ( i < j ) is true, So the answer should not be 'i' but 'j'.
i.e. ( MinArray[i] = j ) in this case.
the other way, i.e. if I want to have ( MinArray[i] = i ) in the above case
I could have modified some of the lines as ..
- Code:
Initialize { MinArray[i=(0 to n-1)] = i };
- Code:
for( every possible vertex j from i )
{
if(color[i]==1)
MinArray[i] = minimum( MinArray[i], j );
else
MinArray[i] = minimum( MinArray[i], mod-DFS(j) );
}
Dee2306- Posts : 3
Join date : 2009-01-31
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|