mockers
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Graph reachability Ques

3 posters

Go down

Graph reachability Ques Empty Graph reachability Ques

Post  shivang Tue Feb 03, 2009 11:40 am

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.... Twisted Evil cheers
shivang
shivang

Posts : 42
Join date : 2009-01-30
Age : 35

Back to top Go down

Graph reachability Ques Empty Re: Graph reachability Ques

Post  Dee2306 Fri Feb 06, 2009 11:09 am

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
Dee2306

Posts : 3
Join date : 2009-01-31

Back to top Go down

Graph reachability Ques Empty @shivang

Post  ballu Sat Feb 07, 2009 2:11 am

shivang sir bataiye sahi hai ya galat
ballu
ballu

Posts : 58
Join date : 2009-02-01

Back to top Go down

Graph reachability Ques Empty Re: Graph reachability Ques

Post  Dee2306 Sat Feb 07, 2009 4:34 pm

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 ..

Code:

Initialize { MinArray[i=(0 to n-1)] = i  };
and

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) );
}
That's It.
Dee2306
Dee2306

Posts : 3
Join date : 2009-01-31

Back to top Go down

Graph reachability Ques Empty Re: Graph reachability Ques

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum