Experiment Name: Name: Tushar Shingade Roll No: 4253 Batch: B-3 Program
Experiment Name: Name: Tushar Shingade Roll No: 4253 Batch: B-3 Program
clc; clear all; close all; %% Create and view a directed graph with 6 nodes and 11 edges. W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21]; DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W);
%% Finding the Shortest Path in an Undirected Graph %Create and view an undirected graph with 6 nodes and 11 edges. UG = tril(DG + DG'); %% h = view(biograph(UG,[],'ShowArrows','off','ShowWeights','on')); %% Find the shortest path in the graph from node 1 to node 6. [dist,path,pred] = graphshortestpath(UG,1,6,'directed',false); %% Mark the nodes and edges of the shortest path by coloring them red and % increasing the line width. set(h.Nodes(path),'Color',[1 0.4 0.4]); fowEdges = getedgesbynodeid(h,get(h.Nodes(path),'ID')); revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID')); edges = [fowEdges;revEdges]; set(edges,'LineColor',[1 0 0]); set(edges,'LineWidth',1.5);
Output: