| GraphDemo.java |
1 /****************************************************************************
2 **
3 ** This file is part of yFiles-2.9.
4 **
5 ** yWorks proprietary/confidential. Use is subject to license terms.
6 **
7 ** Redistribution of this file or of an unauthorized byte-code version
8 ** of this file is strictly forbidden.
9 **
10 ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28,
11 ** 72070 Tuebingen, Germany. All rights reserved.
12 **
13 ***************************************************************************/
14 package demo.base;
15
16 import y.base.Graph;
17 import y.base.Node;
18 import y.base.Edge;
19 import y.base.NodeCursor;
20 import y.base.EdgeCursor;
21
22 import y.base.NodeMap;
23 import y.base.EdgeMap;
24
25
26 /**
27 * Demonstrates how to use the directed graph data type Graph.
28 * <p>
29 * <b>usage:</b> java demo.base.GraphDemo
30 * </p>
31 *
32 * <a href="http://docs.yworks.com/yfiles/doc/developers-guide/base.html#Creating Graphs and Graph Elements">Section Creating Graphs and Graph Elements</a> in the yFiles for Java Developer's Guide
33 */
34 public class GraphDemo
35 {
36 public GraphDemo()
37 {
38 //instantiates an empty graph
39 Graph graph = new Graph();
40
41 //create a temporary node array for fast lookup
42 Node[] tmpNodes = new Node[5];
43
44 //create some nodes in the graph and store references in the array
45 for(int i = 0; i < 5; i++)
46 {
47 tmpNodes[i] = graph.createNode();
48 }
49
50 //create some edges in the graph
51 for(int i = 0; i < 5; i++)
52 {
53 for(int j = i+1; j < 5; j++)
54 {
55 //create an edge from node at index i to node at index j
56 graph.createEdge(tmpNodes[i],tmpNodes[j]);
57 }
58 }
59
60
61 //output the nodes of the graph
62 System.out.println("The nodes of the graph");
63 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
64 {
65 Node node = nc.node();
66 System.out.println(node);
67 System.out.println("in edges #" + node.inDegree());
68 for(EdgeCursor ec = node.inEdges(); ec.ok(); ec.next())
69 {
70 System.out.println(ec.edge());
71 }
72 System.out.println("out edges #" + node.outDegree());
73 for(EdgeCursor ec = node.outEdges(); ec.ok(); ec.next())
74 {
75 System.out.println(ec.edge());
76 }
77 }
78
79
80 //output the edges of the graph
81 System.out.println("\nThe edges of the graph");
82 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
83 {
84 System.out.println(ec.edge());
85 }
86
87 //reverse edges that have consecutive neighbors in graph
88 //reversing means switching source and target node
89 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
90 {
91 if(Math.abs(ec.edge().source().index() - ec.edge().target().index()) == 1)
92 graph.reverseEdge(ec.edge());
93 }
94
95 System.out.println("\nthe edges of the graph after some edge reversal");
96 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
97 {
98 System.out.println(ec.edge());
99 }
100
101 ///////////////////////////////////////////////////////////////////////////
102 // Node- and EdgeMap handling ///////////////////////////////////////////
103 ///////////////////////////////////////////////////////////////////////////
104
105 //create a nodemap for the graph
106 NodeMap nodeMap = graph.createNodeMap();
107 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
108 {
109 //get node at current cursor position
110 Node node = nc.node();
111 //associate descriptive String to the node via a nodemap
112 nodeMap.set(node,"this is node " + node.index());
113 }
114
115 //create an edgemap for the graph
116 EdgeMap edgeMap = graph.createEdgeMap();
117 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
118 {
119 //get edge at current cursor position
120 Edge edge = ec.edge();
121 //associate descriptive String to the edge via an edgemap
122 edgeMap.set(edge,"this is edge [" +
123 nodeMap.get(edge.source()) + "," +
124 nodeMap.get(edge.target()) + "]");
125 }
126
127 //output the nodemap values of the nodes
128 System.out.println("\nThe node map values of the graph");
129 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
130 {
131 System.out.println(nodeMap.get(nc.node()));
132 }
133
134 //output the edges of the graph
135 System.out.println("\nThe edge map values of the graph");
136 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
137 {
138 System.out.println(edgeMap.get(ec.edge()));
139 }
140
141 //cleanup unneeded node and edge maps again (free resources)
142 graph.disposeNodeMap(nodeMap);
143 graph.disposeEdgeMap(edgeMap);
144
145 ///////////////////////////////////////////////////////////////////////////
146 // removing elements from the graph //////////////////////////////////////
147 ///////////////////////////////////////////////////////////////////////////
148
149 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
150 {
151 //remove node that has a edge degree > 2
152 if(nc.node().degree() > 2)
153 {
154 //removed the node and all of its adjacent edges from the graph
155 graph.removeNode(nc.node());
156 }
157 }
158 System.out.println("\ngraph after some node removal");
159 System.out.println(graph);
160
161
162
163 }
164
165 public static void main(String[] args)
166 {
167 new GraphDemo();
168 }
169 }
170