Вы находитесь на странице: 1из 3

//En .net Imports System.Collections.

Generic Public Overridable Function findShortestRoute(ByVal destination As String, ByVal parentNode As Ciudad) As List(Of Ciudad) Dim route As List(Of Ciudad) = newArrayList(Of Ciudad)() 'inicializa la ruta 'CASO BASE : cuando encuentra el nodo destinoif (Me.name.equalsIgnoreCase(destination)) route.Add(Me) Return route Else For i As Integer = 0 To nextNodes.size() - 1 'CASO RECURSIVO: expander nodo siguiente solo si no es nodo padreif ((Not nextNodes.get(i).Parent)) parentNode.Parent = True 'le asigna la bandera parent al nodo padre. //expande recursivamente el siguiente nodo en la lista.ArrayList<Ciudad> newRoute = nextNo des.get(i).findShortestRoute( destination, Me) If (Not newRoute.Empty) AndAlso (newRoute.size() < route.Count OrElse route.Coun t = 0) Then 'actualiza la la ruta si la nueva ruta es menor a la anterior o es e l primer caso con xito.route = newRoute;} Me.parent = False 'restaura la bandera parent antes de regresar a un nivel anter ior. End If If route.Count > 0 Then 'si es caso de exito agrega la ciudad actual a la ruta y la regresa.route.add( Me) End If Return route Next i

/////////////////////////////////////////// // //////Pseudocdigo: Funcin Lista shortestRoute ( destino , nodoPadre ){Lista ruta //caso base IF ( this = destino ){ ruta.add(this) Return ruta }ELSE { //caso recursivo For( i=0 ; i < this.nodos.size() ; i++

){IF( this.nodos(i).esPadre = false ){ nodoPadre.setParent(true);nuevaRuta= shortestRoute(destino , this)IF( nuevaRuta.isNotEmpty AND (nuevaRuta.size < ruta.size OR ruta.isEmpty ){ ruta=nuevaRuta }}}IF( ruta.isNotEmpty ){Ruta.add(this)}Return ruta }} /////////Cdigo en Java: public ArrayList<Ciudad>findShortestRoute(String destination,Ciudad parentNode) {ArrayList<Ciudad> route = newArrayList<Ciudad>(); //inicializa la ruta //CASO BASE : cuando encuentra el nodo destinoif ( this .name.equalsIgnoreCase( destination )) {route.add( this ); return route;} else { for ( int i = 0; i < nextNodes.size(); i++) //CASO RECURSIVO: expander nodo siguiente solo si no es nodo padreif (!nextNodes.get(i).isParent()) { parentNode .setParent( true ); //le asigna la bandera parent al nodo padre. //expande recursivamente el sig uiente nodo en la lista.ArrayList<Ciudad> newRoute = nextNodes.get(i).findShorte stRoute( destination , this ); if (!newRoute.isEmpty() && (newRoute.size() < route.size() || route.isEmpty())) //a ctualiza la la ruta si la nueva ruta es menor a la anterior o es el primer caso con xito.route = newRoute;} this .parent = false ; //restaura la bandera parent antes de regresar a un nivel anterior.

if (!route.isEmpty()) //si es caso de exito agrega la ciudad actual a la ruta y la regresa.route.add( this ); return route;} }

Вам также может понравиться