Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Grupo: 3G-2
Fecha: 28/01/2017
OBJETIVO PRINCIPAL
Demostrar la factibilidad al uso de comandos de Matlab en el empleo de grafos
OBJETIVO SECUNDARIO
Programar de forma que un gráfico sea realizado con el comando PRIM
Encontrar la distancia más corta por medio del comando DIJKSTRA
CODIGO DE PROGRAMACION
Ejercicio 1
Script guía
clear;
clc;
W = [0 4 6 8 3 7 4 1 8 3 9 8 6 7 3 4 2 4 2 6 8 5 5 1 7];
DG = sparse([15 1 2 2 1 4 4 3 6 6 5 5 7 8 8 9 9 10 11 11 11 12 12
13 14],...
[1 2 6 3 4 5 7 5 10 5 9 2 8 9 11 10 12 13 12 14 15 13
14 14 15],W);
h = view(biograph(DG,[],'ShowWeights','on'));
[dist,path,pred] = graphshortestpath(DG,1,15);
set(h.Nodes(path),'Color',[1 0.4 0.4]);
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0]);
set(edges,'LineWidth',1.5);
Ejercicio 2
clc;
clear all;
close all;
for i=1:1:25
w(i)=input(['ingrese el peso(',num2str(i),')\n']);
end
DG= sparse([15 1 2 2 1 4 4 3 6 6 5 5 7 8 8 9 9 10 11 11 11 12 12 13
14], ...
[1 2 3 4 6 5 7 5 10 5 9 2 8 9 11 10 12 13 12 14 15 13 14 14 15],
w);
h=view(biograph (DG,[],'ShowWeights','on'));
[dist,path,pred] =graphshortestpath(DG,1,15);
set(h.Nodes(path),'color',[1 0.4 0.4]);
edges = getedgesbynodeid (h,get(h.Nodes (ruta),'ID'));
set (edges, 'lineColor', [1 0 0]);
set(edges, 'LineWidth', 1.5);
Ejercicio 3
Ruta corta
clc;
clear all;
close all;
for i=1:1:tot
w(i)=input(['ingrese el peso(',num2str(i),')\n']);
end
tot=input('ingrese el numero de rutas \n');
for i=1:1:tot
x(i)=input(['ingrese el inicio de la (',num2str(i),'º)ruta
\n']);
y(i)=input(['ingrese el fin de la (',num2str(i),'º)ruta \n']);
end
DG= sparse(x,y,w);
h=view(biograph (DG,[],'ShowWeights','on'));
[dist,path,pred] =graphshortestpath(DG,1,15);
set(h.Nodes(path),'color',[1 0.4 0.4]);
edges = getedgesbynodeid (h,get(h.Nodes (ruta),'ID'));
set (edges, 'lineColor', [1 0 0]);
set(edges, 'LineWidth', 1.5);
disp(path)
Ejercicio 4
Ruta corta
clc;
clear all;
close all;
Tot=input('ingrese el numero de rutas \n');
for i=1:1:Tot
x(i)=input(['ingrese el inicio de la (',num2str(i),'º)ruta
\n']);
y(i)=input(['ingrese el fin de la (',num2str(i),'º)ruta \n']);
end
for i=1:1:Tot
w(i)=input(['ingrese el peso(',num2str(i),')\n']);
end
DG= sparse(x,y,w);
a=input('ingrese cual es nodo inicio\n');
b=input('ingrese cual es nodo fin \n');
h=view(biograph (DG,[],'ShowWeights','on'));
[dist,path,pred] =graphshortestpath(DG,1,15);
set(h.Nodes(path),'color',[1 0.4 0.4]);
edges = getedgesbynodeid (h,get(h.Nodes (ruta),'ID'));
set (edges, 'lineColor', [1 0 0]);
set(edges, 'LineWidth', 1.5);
fprinrf('la distancia que es mas corta es: %i \n',dist)
fprintf('la ruta es:')
disp(path)
VISUALIZACION
Ejercicio 1
Ejercicio 2
Ejercicio 3
Ejercicio 4
CONCLUSIONES
Se puede observar que en cada iteración del algoritmo empleado, se resuelve
un problema de la ruta más corta, con la restricción de que las longitudes de
los arcos son no negativas.