where:

(There is no train/airplane) connection between cities that are not connected by an edge)

Answer:
with cost = 3 + 2 + 1 = 6
Example:



Note:

Warning:


boolean visited[ ]; // denote whether a node has been visited visited = new boolean[ # nodes ]; 

Set all nodes to "not visited"; put an initial node into "toVisitNodes"; while ( "toVisitNodes" ≠ empty ) do { x = select (and delete) some node from "toVisitNodes"; if ( x has not been visited ) { visited[x] = true; // Visit node x ! for ( every edge (x, y) /* we are using all edges ! */ ) if ( y has not been visited ) add y to "toVisitNodes"; // Use the edge (x,y) !!! } } 
Set all nodes to "not visited"; We start the traversal at node 0 (i.e., insert initial node = 0 into "toVisitNodes") 
Graphically:


(Too long to go through all the steps !)
/* ============================================== Initialize variables for the traversal ============================================== */ for ( i = 0; i < NNodes; i++ ) { visited[i] = false; } add( 0, toVisitNodes); // Start the "to visit" at node 0 /* =========================================== Loop as long as there are "active" node =========================================== */ while( toVisitNodes ≠ empty ) { int nextNode; // Next node to visit int i; nextNode = remove( toVisitNodes ); // Remove a node from "toVisitNodes" if ( ! visited[nextNode] ) { visited[nextNode] = true; // Mark node as visited System.out.println("nextNode = " + nextNode ); for ( i = 0; i < NNodes; i++ ) if ( adjMatrix[nextNode][i] > 0 && ! visited[i] ) add( i, toVisitNodes); } } 


