AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
TraversalExample.java
Go to the documentation of this file.
1package aud.example.graph;
2
3import java.io.File;
4
5import aud.util.*;
6
8public class TraversalExample extends SingleStepper {
9 protected DotViewer viewer_ = DotViewer.displayWindow
10 ((String) null,"aud.example.graph.TraversalExample");
11
13 public Traversal traversal = null;
14
17 super("aud.example.graph.TraversalExample");
18 }
19
21 public MyGraph graph() { return traversal!=null ? traversal.graph() : null; }
22
23 @Override protected void onHalt() {
24 if (graph()!=null)
26 }
27
28 protected static void usage() {
29 System.err.println(
30 "java aud.example.graph.TraversalExample "+
31 "[-g graphfile] [-m method] [-s s0] [-d s1] [-v N]\n"+
32 " -g FILE read graph from FILE\n"+
33 " -m METHOD traversal {dfs,idfs1,idfs2,bfs,dijkstra,prim,astar}\n"+
34 " -s START start node\n"+
35 " -n N show result of every n-th step\n"+
36 " -v V set verbose level (extra output)\n"+
37 " -d DESTINATION destination required for A* algorithm\n");
38 System.exit(-1);
39 }
40
42 public static void main(String[] args) {
43 MyGraph g=new MyGraph(false);
44
45 File graph=null;
46 String method="dfs";
47 String start=null;
48 String destination=null;
49 int n=1;
50 int verbose=0;
51
52 for (int i=0;i<args.length;++i) {
53 if (args[i].compareTo("-g")==0)
54 graph=new File(args[++i]);
55 else if (args[i].compareTo("-m")==0)
56 method=args[++i];
57 else if (args[i].compareTo("-s")==0)
58 start=args[++i];
59 else if (args[i].compareTo("-n")==0)
60 n=Integer.parseInt(args[++i]);
61 else if (args[i].compareTo("-v")==0)
62 verbose=Integer.parseInt(args[++i]);
63 else if (args[i].compareTo("-d")==0)
64 destination=args[++i];
65 else
66 usage();
67 }
68
70
71 if (graph==null)
72 g=new GraphP88(method.compareTo("dfs")!=0 && method.compareTo("bfs")!=0);
73 else {
75 }
76
77 Traversal algorithm=null;
78
79 if (method.compareTo("dfs")==0)
80 algorithm=new DepthFirstSearch(g);
81 else if (method.compareTo("idfs1")==0)
82 algorithm=new IterativeDFS1(g);
83 else if (method.compareTo("idfs2")==0)
84 algorithm=new IterativeDFS2(g);
85 else if (method.compareTo("bfs")==0)
86 algorithm=new BreadthFirstSearch(g);
87 else if (method.compareTo("dijkstra")==0)
88 algorithm=new DijkstraShortestPaths(g);
89 else if (method.compareTo("prim")==0)
90 algorithm=new PrimMinimumSpanningTree(g);
91 else if (method.compareTo("astar")==0)
92 algorithm=new AStarShortestPath(g);
93
94 if (algorithm==null)
95 usage();
96
97 algorithm.singlestepper=app;
98 algorithm.nsteps=n;
99 algorithm.verbose=verbose;
100
101 app.traversal=algorithm;
102
103 MyNode s0=null;
104 MyNode s1=null;
105
106 if (start!=null) {
107 for (MyNode node : g)
108 if (node.getLabel().compareTo(start)==0) {
109 s0=node;
110 break;
111 }
112 if (s0==null)
113 System.err.println("could not find start node s0='"+start+"'");
114 }
115 if (destination!=null) {
116 for (MyNode node : g)
117 if (node.getLabel().compareTo(destination)==0) {
118 s1=node;
119 break;
120 }
121 if (s1==null)
122 System.err.println("could not find destination node s1='"+destination+"'");
123 }
124
125 if (s0==null)
126 s0=app.graph().getSomeNode();
127
128 if (method.compareTo("astar")==0) {
129 if (s1==null)
130 usage();
131
132 s1.color="red"; // destination
133
134 app.halt(algorithm.name()+" from "+s0+" to "+s1);
135
136 // start traversal
137 ((AStarShortestPath) algorithm).start(s0,s1);
138
139 // show result from backtracking
140 app.halt("shortest path");
141 }
142 else {
143 app.halt(algorithm.name()+ " from "+s0);
144
145 // start traversal
146 algorithm.start(s0);
147 }
148
149
150 ((SimpleDecorator) app.graph().getDecorator()).highlightNode(null);
151 ((SimpleDecorator) app.graph().getDecorator()).highlightEdge(null);
152
153 app.halt("DONE");
154
155 System.out.println(app.graph());
156
157 System.exit(0);
158 }
159}
implementation of the A* algorithm
Standard recursive implementation of depth first search.
implements Dijkstra's algorithm by defining priority
undirected (weighted or unweighted )example graph (Sedgewick, Algorithms in Java.
Definition: GraphP88.java:7
Parse text to build graph.
void parse(String input)
parse input
Iterative implementation of DFS.
iterative implementation of DFS Traversal like DepthFirstSearch but as for IterativeDFS1 the order of...
graph based on aud.graph.GraphAM
Definition: MyGraph.java:11
GraphvizDecorator getDecorator()
Definition: MyGraph.java:34
AbstractGraph< AbstractNode, AbstractEdge > getAbstractGraph()
view this graph as an AbstractGraph
Definition: MyGraph.java:29
node with all possible attributes that we require ;-)
Definition: MyNode.java:6
String color
color as string
Definition: MyNode.java:27
implements Prim's algorithm by defining priority
MyGraph graph()
get graph (associated with traversal )
Traversal traversal
the particular traversal algorithm
static void main(String[] args)
example (see also usage)
interface for traversals of MyGraph
Definition: Traversal.java:6
abstract String name()
get traversal name
SingleStepper singlestepper
may halt if single stepper was set
Definition: Traversal.java:11
int verbose
set verbosity (extra output if >0)
Definition: Traversal.java:15
int nsteps
halt every nsteps steps in time_
Definition: Traversal.java:13
abstract void start(MyNode s0)
start traversal at node s0
Node getSomeNode()
Definition: GraphAM.java:74
Simple viewer for Graphvizable.
Definition: DotViewer.java:47
void display(String code)
display dot code
Definition: DotViewer.java:107
Example for a simple decorator.
Simple framework for single stepping code.
void halt(String text, int timeout)
display text and wait for user or timeout
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1