AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.example.graph.TraversalExample Class Reference

graph traversal example More...

+ Inheritance diagram for aud.example.graph.TraversalExample:
+ Collaboration diagram for aud.example.graph.TraversalExample:

Public Member Functions

 TraversalExample ()
 create instance More...
 
MyGraph graph ()
 get graph (associated with traversal ) More...
 
- Public Member Functions inherited from aud.util.SingleStepper
 SingleStepper (JFrame parent)
 create new instance More...
 
 SingleStepper (String caption)
 create new instance More...
 
JFrame parent ()
 get parent widget More...
 
void halt (String text, int timeout)
 display text and wait for user or timeout
More...
 
void setTimeout (int timeout)
 Set global timeout. More...
 
SingleStepper whereAmI ()
 print location of calling code More...
 
SingleStepper showSource ()
 jmp to caller's location in editor (emacs only) More...
 
void halt (String text)
 display text and wait for user (or global timeout) More...
 
void halt ()
 wait for user More...
 

Static Public Member Functions

static void main (String[] args)
 example (see also usage) More...
 
static void main (String[] args)
 

Public Attributes

Traversal traversal = null
 the particular traversal algorithm More...
 

Protected Member Functions

void onHalt ()
 
- Protected Member Functions inherited from aud.util.SingleStepper
JComponent createComponents ()
 
void onNext ()
 call on button pressed More...
 
void println (String text)
 print to both, text area and stdout More...
 
void onHalt ()
 

Static Protected Member Functions

static void usage ()
 

Protected Attributes

DotViewer viewer_
 
- Protected Attributes inherited from aud.util.SingleStepper
JFrame frame
 
JTextArea history
 
JButton next
 
Object monitor = new Object()
 
int timeout = 0
 

Detailed Description

graph traversal example

Definition at line 8 of file TraversalExample.java.

Constructor & Destructor Documentation

◆ TraversalExample()

create instance

Definition at line 16 of file TraversalExample.java.

16 {
17 super("aud.example.graph.TraversalExample");
18 }

Referenced by aud.example.graph.TraversalExample.main().

+ Here is the caller graph for this function:

Member Function Documentation

◆ graph()

MyGraph aud.example.graph.TraversalExample.graph ( )

get graph (associated with traversal )

Definition at line 21 of file TraversalExample.java.

21{ return traversal!=null ? traversal.graph() : null; }
Traversal traversal
the particular traversal algorithm

References aud.example.graph.TraversalExample.traversal.

Referenced by aud.example.graph.TraversalExample.main(), and aud.example.graph.TraversalExample.onHalt().

+ Here is the caller graph for this function:

◆ main()

static void aud.example.graph.TraversalExample.main ( String[]  args)
static

example (see also usage)

Reimplemented from aud.util.SingleStepper.

Definition at line 42 of file TraversalExample.java.

42 {
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 {
74 new GraphParser(g.getAbstractGraph()).parse(graph);
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 }
MyGraph graph()
get graph (associated with traversal )
Example for a simple decorator.

References aud.example.graph.MyNode.color, aud.example.graph.MyGraph.getAbstractGraph(), aud.example.graph.MyGraph.getDecorator(), aud.graph.GraphAM< Node extends AbstractNode, Edge extends AbstractEdge >.getSomeNode(), aud.example.graph.TraversalExample.graph(), aud.util.SingleStepper.halt(), aud.example.graph.Traversal.name(), aud.example.graph.Traversal.nsteps, aud.example.graph.GraphParser.parse(), aud.example.graph.Traversal.singlestepper, aud.example.graph.Traversal.start(), aud.example.graph.TraversalExample.traversal, aud.example.graph.TraversalExample.TraversalExample(), aud.example.graph.TraversalExample.usage(), and aud.example.graph.Traversal.verbose.

+ Here is the call graph for this function:

◆ onHalt()

void aud.example.graph.TraversalExample.onHalt ( )
protected

Reimplemented from aud.util.SingleStepper.

Definition at line 23 of file TraversalExample.java.

23 {
24 if (graph()!=null)
26 }
void display(String code)
display dot code
Definition: DotViewer.java:107

References aud.util.DotViewer.display(), aud.example.graph.TraversalExample.graph(), and aud.example.graph.TraversalExample.viewer_.

+ Here is the call graph for this function:

◆ usage()

static void aud.example.graph.TraversalExample.usage ( )
staticprotected

Definition at line 28 of file TraversalExample.java.

28 {
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 }

Referenced by aud.example.graph.TraversalExample.main().

+ Here is the caller graph for this function:

Member Data Documentation

◆ traversal

Traversal aud.example.graph.TraversalExample.traversal = null

the particular traversal algorithm

Definition at line 13 of file TraversalExample.java.

Referenced by aud.example.graph.TraversalExample.graph(), and aud.example.graph.TraversalExample.main().

◆ viewer_

DotViewer aud.example.graph.TraversalExample.viewer_
protected
Initial value:
= DotViewer.displayWindow
((String) null,"aud.example.graph.TraversalExample")

Definition at line 9 of file TraversalExample.java.

Referenced by aud.example.graph.TraversalExample.onHalt().


The documentation for this class was generated from the following file: