Sunteți pe pagina 1din 4

program dijkstra(input, output); (* Implementacion del algoritmo de Dijkstra para encontrar un camino mas corto entre dos vertices

en un grafo pesado. Entrada: un grafo pesado dado por lista de aristas, el numero de vertices, n, y los vertices a unir, s y t. Los vertices son todos los enteros positivos entre 1 y n. Si alguno no aparece en ninguna arista, es un vertice aislado. Los pesos de cada arista son enteros positivos. Salida: la longitud de un camino mas corto, y el camino que lo realiza como lista de vertices. Si los vertices no se pueden conectar, se imprime un mensaje. *) const MAXN = 20; (* maximo numero de vertices *) MAXM = 100; (* maximo numero de aristas *) type costo = integer; (* podria ser real *) arreglodevertices = array[1..MAXN] of integer; tipoarista = record i, j: integer; (* los extremos *) w: costo (* el costo *) end; arreglodearistas = array[1..MAXM] of tipoarista; matrizNN = array[1..MAXN,1..MAXN] of costo; var ngrafo, mgrafo, s, t: integer; infinito: costo; padre: arreglodevertices; dist: array[1..MAXN] of costo; aristasgrafo: arreglodearistas; costos: matrizNN; function nuevaarista: boolean; begin writeln; write(' Entrar la arista ', mgrafo:2); writeln(' (fin = <retorno>): '); write(' Entrar el primer vertice: '); if (not eoln) then begin nuevaarista := true; with aristasgrafo[mgrafo] do begin read(i); write(' Entrar el segundo vertice: '); read(j); write(' Entrar el costo (entero > 0): '); readln(w) end (* with *) end (* if not eoln *) else begin nuevaarista := false; readln end end;

procedure entrardatos; begin (* vertices *) writeln; write('Entrar el numero de vertices: '); readln(ngrafo); (* aristas *) writeln; writeln('Entrar las aristas:'); mgrafo := 1; while (nuevaarista) do mgrafo := mgrafo + 1; mgrafo := mgrafo - 1; (* vertices a unir *) writeln; write('Entrar el vertice de partida: '); readln(s); write('Entrar el vertice de llegada: '); readln(t) end; procedure escribiraristas(a: arreglodearistas; m: integer); var k: integer; begin writeln; writeln(' Vertices Costo'); for k := 1 to m do with a[k] do writeln(i:5, j:6, w:9) end; procedure dearistasacostos; var i, j, k: integer; begin (* infinito = numero mas grande que suma de costos *) infinito := 1; for k := 1 to mgrafo do infinito := infinito + aristasgrafo[k].w; for i := 1 to ngrafo do for j := 1 to ngrafo do costos[i,j] := infinito; for k := 1 to mgrafo do with aristasgrafo[k] do begin costos[i,j] := w; costos[j,i] := w end end; procedure mascorto; var i, j, k, kmin, hay: integer; d, dmin: costo; avisitar: arreglodevertices; begin (* inicializacion *) dearistasacostos; for i := 1 to ngrafo do begin padre[i] := 0; dist[i] := infinito end; (* s es la "raiz" y el unico en la cola al comenzar *) padre[s] := s; dist[s] := 0; hay := 1; avisitar[1] := s; repeat (* aca hay > 0 *)

(* nuevo i: el de minima distancia en la cola *) kmin := hay; i := avisitar[hay]; dmin := dist[i]; for k := 1 to hay - 1 do begin j := avisitar[k]; d := dist[j]; if (d < dmin) then begin kmin := k; i := j; dmin := d end end; (* ahora tenemos el nuevo i *) if (i <> t) then begin (* poner i al fin de la cola y sacarlo *) avisitar[kmin] := avisitar[hay]; hay := hay - 1; (* examinar vecinos de i *) for j := 1 to ngrafo do if (dist[i] + costos[i,j] < dist[j]) then begin (* si j no se agrego, agregarlo a la cola *) if (dist[j] = infinito) then begin hay := hay + 1; avisitar[hay] := j end; (* actualizar dist y padre *) dist[j] := dist[i] + costos[i,j]; padre[j] := i end end (* if i <> t *) until ((i = t) or (hay = 0)) end; procedure hacercamino; (* Construir e imprimir el camino mas corto usando padre *) const maxrenglon = 10; (* maxima cantidad / renglon *) var i, ncamino: integer; camino: arreglodevertices; begin ncamino := 1; camino[1] := t; i := t; while (i <> s) do begin i := padre[i]; ncamino := ncamino + 1; camino[ncamino] := i end; (* imprimir "dando vuelta" el arreglo *); writeln(' Los vertices en el camino son:'); for i := ncamino downto 1 do begin write(camino[i]:5); if ((i mod maxrenglon) = 0) then writeln end; if ((ncamino mod maxrenglon) <> 0) then writeln end; begin (* Carteles *) writeln('** Busqueda del camino mas corto entre dos'); writeln(' vertices con el algoritmo de Dijkstra.'); writeln(' El grafo se entra mediante lista de aristas.'); (* Datos e inicializacion *) entrardatos; writeln; writeln('Las aristas del grafo son:'); escribiraristas(aristasgrafo, mgrafo); writeln; writeln('Los vertices a unir son:'); writeln(' partida: ', s:1);

writeln(' llegada: ', t:1); (* Procesamiento y salida *) mascorto; writeln; if (padre[t] > 0) then begin writeln(' La longitud del camino es ', dist[t]:1); hacercamino end else begin writeln; writeln('** ', s:1, ' y ' , t:1, ' no se conectan **') end; (* Fin *) writeln; writeln('** Fin **') end.

S-ar putea să vă placă și