CODE-BTL
CODE-BTL
function dynamic_a_star_with_partial_connectivity()
% Initialize variables
nodes = {}; % Node labels
positions = []; % Node coordinates
edges = []; % Edges (from-to pairs)
weights = []; % Corresponding weights
node_count = 0;
% Visualization setup
figure;
hold on;
axis equal;
grid on;
title('Dynamic Graph Visualization with A* Algorithm');
xlabel('X');
ylabel('Y');
% Parse input
coords = sscanf(user_input, '%f,%f');
if length(coords) ~= 2
disp('Invalid input. Please enter coordinates as x,y.');
continue;
end
% Add node
node_count = node_count + 1;
nodes{node_count} = sprintf('%c', 'A' + node_count - 1); % Node label
positions = [positions; coords']; % Add coordinates
if isempty(start_index) || isempty(goal_index)
error('Invalid start or goal node.');
end
% Initialize A* variables
num_nodes = length(nodes);
open_list = [start_index];
closed_list = [];
g_costs = inf(1, num_nodes); % Actual cost from start
g_costs(start_index) = 0;
f_costs = inf(1, num_nodes); % Total cost (g + h)
f_costs(start_index) = heuristic_distance(positions(start_index, :), positions(goal_index, :));
predecessors = zeros(1, num_nodes); % To trace the path
% Perform A* algorithm
while ~isempty(open_list)
% Find the node in the open list with the smallest f_cost
[~, current_idx] = min(f_costs(open_list));
current_node = open_list(current_idx);
open_list(current_idx) = []; % Remove from open list
closed_list = [closed_list, current_node]; % Add to closed list
% Explore neighbors
for i = 1:size(edges, 2)
% Check if there's an edge from the current node
if edges(1, i) == current_node
neighbor = edges(2, i);
elseif edges(2, i) == current_node
neighbor = edges(1, i);
else
CODE
continue;
end