Skip to content
Snippets Groups Projects
Commit ca625998 authored by manxilin's avatar manxilin
Browse files

update UAS

parent aa4beedf
No related branches found
No related tags found
No related merge requests found
function [route, logs, g] = Dijkstra(start_node, end_node, map, D)
function [route, logs, G] = Dijkstra(start_node, end_node, map, D)
% Dijkstra's algorithm
%//////////////////////////////////////////////////////////////////////////
% (int) start_node
......@@ -21,6 +21,7 @@ P = flag;
% distance vector
g = Inf(1, size(map, 1));
g(1) = 0;
G = [];
while ~isempty(s.data)
% pop the frontier
[front, s] = s.pop();
......@@ -45,6 +46,7 @@ while ~isempty(s.data)
nodes = nodes(idx);
% stack the nodes
s = s.push(nodes);
G = [G;g];
end
% find route
route = findRoute(end_node, P);
......
......@@ -54,9 +54,9 @@ function nodes = getNext(front, map, flag, nodes)
for i = 1:size(map, 1)
if ~flag(i) && map(front, i)
% start by stacking the lowest of the s numbers
nodes = [nodes, i];
%nodes = [nodes, i];
% start by stacking the highest of the s numbers
%nodes = [i, nodes];
nodes = [i, nodes];
end
end
end
\ No newline at end of file
%========================================================
% @author: mxl
% @date: 06/15/2020
%--------------------------------------------------------
%%
clear; close all; clc;
addpath('/Gathering-of-lectures-and-exercises/31390/part4/Astar/');
%%
%// draw the graph
s = [0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, ...
6, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12, 13, 14, ...
14, 15, 15, 16, 17, 17, 17, 18, 19, 19, 20, 22, 23, 23]+1;
t = [1, 2, 3, 4, 2, 5, 6, 7, 8, 5, 9, ...
10, 6, 11, 7, 11, 12, 13, 13, 14, 15, 16, 23, 18, 15, ...
18, 19, 20, 21, 12, 19, 23, 17, 20, 23, 22, 21, 21, 22]+1;
G = graph(s, t);
figure;
plot(G);
%%
%// compress the map in a matrix
M = compress(s, t);
%% Exercise 4.1
%// Depth First Search
% start by stacking the lowest of the s numbers
% start from s0
start_node = 0;
end_node = 23;
[route, logs] = dfs(start_node, end_node, M);
path_len = length(route)-1;
%% Exercise 4.2
%// Breadth First Search
% start by stacking the lowest of the s numbers
% start from s0
start_node = 0;
end_node = 23;
[route, logs] = bfs(start_node, end_node, M);
path_len = length(route)-1;
%%
% from quora
% Advantages of BFS:-
%
% 1. Solution will definitely found out by BFS If there are some solution.
% 2. BFS will never get trapped in blind alley , means unwanted nodes.
% 3. If there are more than one solution then it will find solution with minimal steps.
%
% Disadvantages Of BFS :-
% 1. Memory Constraints As it stores all the nodes of present level to go for next level.
% 2. If solution is far away then it consumes time.
%
% Advantages Of DFS :-
% 1. Memory requirement is Linear WRT Nodes.
% 2. Less time and space complexity rather than BFS.
% 3. Solution can be found out by without much more search.
%
% Disadvantage of DFS :-
% 1. Not Guaranteed that it will give you solution.
% 2. Cut-off depth is smaller so time complexity is more.
% 3. Determination of depth until the search has proceeds.
%
%% Exercise 4.3
%// Dijkstra's algorithm
% generate Adjacency Matrix D
dist = [4.5, 5.4, 2.2, 2.2, 4.2, 3.6, 3.2, 2.2, 3.2, 1.4, 2.2, ...
2, 1.4, 2.2, 2.2, 3.2, 6, 3, 4, 4.1, 6, 3.2, 5.1, 3.2, 1.4, ...
2, 3.2, 2.2, 4, 3.6, 4.1, 3.2, 3.6, 4.5, 2.8, 3.6, 3.6, 2, 3.2];
D = zeros(size(M, 1));
for i = 1:size(s, 2)
D(s(i), t(i)) = dist(i);
D(t(i), s(i)) = dist(i);
end
start_node = 0;
end_node = 23;
[route, logs, g1] = Dijkstra(start_node, end_node, M, D);
%% Exercise 4.4
%// (Greedy) Best First Search
H = [17, 15.5, 11, 15.1, 13.4, 10.8, 10.2, 12.4, 15.2, 8.6, 6.7, 11.7, 5.1, 7.3, ...
6.7, 6.7, 4.5, 3.2, 5.1, 2.8, 7.1, 2, 3.2, 0];
start_node = 0;
end_node = 23;
[route, logs] = greedy(start_node, end_node, M, H);
%% Exercise 4.5
%// A* search algorithm
start_node = 0;
end_node = 23;
[route, logs, g2] = Astar(start_node, end_node, M, H, D);
%% Exercise 4.6
%@http://aandds.com/blog/algorithm-a-star.html
%% Exercise 4.7
% // greedy_2d
map_2d
%% Exercise 4.8
% // greedy_3d
map_3d
%% Exercise 4.11
% // rfbs
[route, logs] = rbfs(start_node, end_node, M, H, D);
%%
figure;
subplot(2,2,1)
for i = 1:6
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,12], 'xlim', [1, 24]);
legend('node0', 'node1', 'node2', 'node3', 'node4',...
'node5');
subplot(2,2,2)
for i = 7:12
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,12], 'xlim', [1, 24]);
legend('node6', 'node7', 'node8', 'node9',...
'node10', 'node11');
%%
figure;
for i = 13:24
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,20], 'xlim', [1, 24]);
legend('''node13',...
'node14', 'node15', 'node16', 'node17', 'node18',...
'node19', 'node20', 'node21', 'node22', 'node23');
%%
function [mtx] = compress(s, t)
% compress the graph into a matrix
assert(size(s, 2)==size(t, 2));
node_num = max(s);
mtx = zeros(node_num, node_num);
for i = 1:size(s, 2)
mtx(s(i), t(i)) = 1;
mtx(t(i), s(i)) = 1;
end
end
\ No newline at end of file
......@@ -95,6 +95,40 @@ map_3d
% // rfbs
[route, logs] = rbfs(start_node, end_node, M, H, D);
%%
figure;
subplot(2,2,1);
for i = 1:6
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,12], 'xlim', [1, 24]);
legend('node0', 'node1', 'node2', 'node3', 'node4','node5');
subplot(2,2,2);
for i = 7:12
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,12], 'xlim', [1, 24]);
legend('node6', 'node7', 'node8', 'node9','node10', 'node11');
subplot(2,2,3);
for i = 13:18
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,20], 'xlim', [1, 24]);
legend('node12', 'node13','node14', 'node15', 'node16', 'node17');
subplot(2,2,4);
for i = 19:24
plot([1:24],g1(:,i)');
hold on;
end
set(gca, 'ylim', [0,20], 'xlim', [1, 24]);
legend('node18','node19', 'node20', 'node21', 'node22', 'node23');
%%
plot(g1(end,:));
xlabel('node');
ylabel('distance');
%%
function [mtx] = compress(s, t)
% compress the graph into a matrix
assert(size(s, 2)==size(t, 2));
......
......@@ -18,7 +18,8 @@ classdef queue
function [d, obj] = pop(obj)
d = obj.data(1);
obj.data = obj.data(2:end);
%obj.data = obj.data(2:end);
obj.data(obj.data==d) = [];
end
end
......
......@@ -18,7 +18,8 @@ classdef stack
function [d, obj] = pop(obj)
d = obj.data(end);
obj.data = obj.data(1:end-1);
%obj.data = obj.data(1:end-1);
obj.data(obj.data==d) = [];
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment