| GraphDemo.java |
1 /****************************************************************************
2 * This demo file is part of yFiles for Java 2.14.
3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4 * 72070 Tuebingen, Germany. All rights reserved.
5 *
6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7 * of demo files in source code or binary form, with or without
8 * modification, is not permitted.
9 *
10 * Owners of a valid software license for a yFiles for Java version that this
11 * demo is shipped with are allowed to use the demo source code as basis
12 * for their own yFiles for Java powered applications. Use of such programs is
13 * governed by the rights and conditions as set out in the yFiles for Java
14 * license agreement.
15 *
16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 ***************************************************************************/
28 package demo.base;
29
30 import y.base.Graph;
31 import y.base.Node;
32 import y.base.Edge;
33 import y.base.NodeCursor;
34 import y.base.EdgeCursor;
35
36 import y.base.NodeMap;
37 import y.base.EdgeMap;
38
39
40 /**
41 * Demonstrates how to use the directed graph data type Graph.
42 * <p>
43 * <b>usage:</b> java demo.base.GraphDemo
44 * </p>
45 *
46 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/base#Creating Graphs and Graph Elements" target="_blank">Section Creating Graphs and Graph Elements</a> in the yFiles for Java Developer's Guide
47 */
48 public class GraphDemo
49 {
50 public GraphDemo()
51 {
52 //instantiates an empty graph
53 Graph graph = new Graph();
54
55 //create a temporary node array for fast lookup
56 Node[] tmpNodes = new Node[5];
57
58 //create some nodes in the graph and store references in the array
59 for(int i = 0; i < 5; i++)
60 {
61 tmpNodes[i] = graph.createNode();
62 }
63
64 //create some edges in the graph
65 for(int i = 0; i < 5; i++)
66 {
67 for(int j = i+1; j < 5; j++)
68 {
69 //create an edge from node at index i to node at index j
70 graph.createEdge(tmpNodes[i],tmpNodes[j]);
71 }
72 }
73
74
75 //output the nodes of the graph
76 System.out.println("The nodes of the graph");
77 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
78 {
79 Node node = nc.node();
80 System.out.println(node);
81 System.out.println("in edges #" + node.inDegree());
82 for(EdgeCursor ec = node.inEdges(); ec.ok(); ec.next())
83 {
84 System.out.println(ec.edge());
85 }
86 System.out.println("out edges #" + node.outDegree());
87 for(EdgeCursor ec = node.outEdges(); ec.ok(); ec.next())
88 {
89 System.out.println(ec.edge());
90 }
91 }
92
93
94 //output the edges of the graph
95 System.out.println("\nThe edges of the graph");
96 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
97 {
98 System.out.println(ec.edge());
99 }
100
101 //reverse edges that have consecutive neighbors in graph
102 //reversing means switching source and target node
103 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
104 {
105 if(Math.abs(ec.edge().source().index() - ec.edge().target().index()) == 1)
106 graph.reverseEdge(ec.edge());
107 }
108
109 System.out.println("\nthe edges of the graph after some edge reversal");
110 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
111 {
112 System.out.println(ec.edge());
113 }
114
115 ///////////////////////////////////////////////////////////////////////////
116 // Node- and EdgeMap handling ///////////////////////////////////////////
117 ///////////////////////////////////////////////////////////////////////////
118
119 //create a nodemap for the graph
120 NodeMap nodeMap = graph.createNodeMap();
121 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
122 {
123 //get node at current cursor position
124 Node node = nc.node();
125 //associate descriptive String to the node via a nodemap
126 nodeMap.set(node,"this is node " + node.index());
127 }
128
129 //create an edgemap for the graph
130 EdgeMap edgeMap = graph.createEdgeMap();
131 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
132 {
133 //get edge at current cursor position
134 Edge edge = ec.edge();
135 //associate descriptive String to the edge via an edgemap
136 edgeMap.set(edge,"this is edge [" +
137 nodeMap.get(edge.source()) + "," +
138 nodeMap.get(edge.target()) + "]");
139 }
140
141 //output the nodemap values of the nodes
142 System.out.println("\nThe node map values of the graph");
143 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
144 {
145 System.out.println(nodeMap.get(nc.node()));
146 }
147
148 //output the edges of the graph
149 System.out.println("\nThe edge map values of the graph");
150 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
151 {
152 System.out.println(edgeMap.get(ec.edge()));
153 }
154
155 //cleanup unneeded node and edge maps again (free resources)
156 graph.disposeNodeMap(nodeMap);
157 graph.disposeEdgeMap(edgeMap);
158
159 ///////////////////////////////////////////////////////////////////////////
160 // removing elements from the graph //////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////////////
162
163 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
164 {
165 //remove node that has a edge degree > 2
166 if(nc.node().degree() > 2)
167 {
168 //removed the node and all of its adjacent edges from the graph
169 graph.removeNode(nc.node());
170 }
171 }
172 System.out.println("\ngraph after some node removal");
173 System.out.println(graph);
174
175
176
177 }
178
179 public static void main(String[] args)
180 {
181 new GraphDemo();
182 }
183 }
184