Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global nodes;
- function DFS(graph, visited, start)
- visited(1, start) = 1;
- mprintf("%d ", start);
- for i=1:nodes
- if graph(start, i) == 1 & visited(1, i) == 0 then
- DFS(graph, visited, i);
- end
- end
- endfunction
- function [from, to]=split(data)
- splitted = evstr(strsplit(data, " "));
- from = splitted(1, 1);
- to = splitted(2, 1);
- endfunction
- nodes = input("How many nodes ? ");
- graph = zeros(nodes, nodes);
- edges = input("How many edges ? ");
- disp("Enter " + string(edges) + " edges (from-to) separated by a white space:");
- for i=1:edges
- in = input("", "string");
- [from, to] = split(in);
- graph(from, to) = 1;
- graph(to, from) = 1;
- end
- startNode = input("Enter initial node : ");
- visited = zeros(1, nodes);
- DFS(graph, visited, startNode);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement