I have got an undirected weighted connected graph. If I select one vertex I automatically select all the vertices connected directly with it. What will be the algorithm so that I am able to select all the vertices of the graph using minimum number of tries? In each try I select one vertex. For example for adjacency matrix a[][]={1,1,0,0,0, 1,1,1,1,0 ,0,1,1,0,0 0,1,0,1,1 0,0,0,1,1} I have to either select vertex 2 and 4 or vertex 2 and 5.
This seems equivalent to the vertex cover problem. It's NP Complete. One of the algorithms that Wikipedia suggests is:
"One algorithmic technique that works here is called bounded search tree algorithm, and its idea is to repeatedly choose some vertex and recursively branch, with two cases at each step: place either the current vertex or all its neighbours into the vertex cover."
edit
here's a proof that solving vertex cover will solve your problem, provided there is no vertex that has no edge:
(*) A solution to vertex cover also solves your problem
For every edge, at least one of the endpoints is selected by vertex cover. If there is at least one edge connected to every vertex, then all vertices end up selected (either directly or by virtue of neighboring a directly-selected vertex), solving the problem.
Note that the converse does not hold:
(*) A solution to your problem does not necessarily solve vertex cover.
consider this graph:
[ ] ----- [X] ¦ ¦ ¦ ¦ [ ] ----- [X]
Directly-selected nodes are marked with an "X". This graph solves your problem as stated, yet the leftmost edge is not covered (so this is not a vertex cover solution).
As already pointed out by red tuna, this is a type of vertex cover problem. Specifically, it's a dominating set problem, the main difference being that a dominating set can be much smaller than a correct vertex cover.
You can model this as a set cover problem. Each vertex is represented as a subset containing itself and all adjoining vertices. If you don't need to guarantee an exact optimal solution (just a very good one), you can then apply the greedy set cover heuristic to achieve an approximation to the optimal set in lg n time.