random path between two nodes in undirected graph

조회 수: 4 (최근 30일)
Sara
Sara 2021년 8월 14일
댓글: Walter Roberson 2021년 8월 15일
I have a graph which consists of nodes and I need a fast algorithm that generates a random path between two nodes. I designed several algorithms from scratch for this but can't seem to get it right.
Either the algorithm gets stuck in loops, or when I keep record of the visited nodes it sometimes gets stuck between visited nodes. Another problem I encountered is that my algorithm was too unstable in performance.
So my question is; does anyone know a fast and stable algorithm for a random path between two reachable nodes in an undirected graph?
  댓글 수: 2
Matt J
Matt J 2021년 8월 14일
We need to see your code in order to say what might be wrong with it.

댓글을 달려면 로그인하십시오.

답변 (1개)

Chunru
Chunru 2021년 8월 15일
편집: Chunru 2021년 8월 15일
% Generate a graph
s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 8 3 7 4 6 5 6 8 7 8];
G = graph(s,t);
h=plot(G)
h =
GraphPlot with properties: NodeColor: [0 0.4470 0.7410] MarkerSize: 4 Marker: 'o' EdgeColor: [0 0.4470 0.7410] LineWidth: 0.5000 LineStyle: '-' NodeLabel: {'1' '2' '3' '4' '5' '6' '7' '8'} EdgeLabel: {} XData: [0.9303 0.2543 1.0175 1.7647 -0.2543 -0.9303 -1.7647 -1.0175] YData: [1.4997 -0.4532 -1.8135 0.0124 0.4532 -1.4997 -0.0124 1.8135] ZData: [0 0 0 0 0 0 0 0] Show all properties
% find all paths between two nodes
p = allpaths(G, 2, 5); % nodes 2 and 5
npath = length(p);
% random path
idx = randi([1 npath], 1);
p{idx}
ans = 1×4
2 1 8 5
highlight(h,p{idx},'NodeColor','g','EdgeColor','g')

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by