1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 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 datatype Graph.
28   * <p>
29   * <b>usage:</b> java demo.base.GraphDemo
30   * </p>
31   */
32  public class GraphDemo 
33  {
34    public GraphDemo()
35    {
36      //instantiates an empty graph
37      Graph graph = new Graph();
38      
39      //create a temporary node array for fast lookup
40      Node tmpNodes[] = new Node[5];
41      
42      //create some nodes in the graph and store references in the array
43      for(int i = 0; i < 5; i++)
44      {
45        tmpNodes[i] = graph.createNode();
46      }
47      
48      //create some edges in the graph
49      for(int i = 0; i < 5; i++)
50      {
51        for(int j = i+1; j < 5; j++)
52        {
53          //create an edge from node at index i to node at index j
54          graph.createEdge(tmpNodes[i],tmpNodes[j]);
55        }
56      }
57      
58      
59      //output the nodes of the graph 
60      System.out.println("The nodes of the graph");
61      for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
62      {
63        Node node = nc.node();
64        System.out.println(node);
65        System.out.println("in edges #" + node.inDegree());
66        for(EdgeCursor ec = node.inEdges(); ec.ok(); ec.next())
67        {
68          System.out.println(ec.edge());
69        }
70        System.out.println("out edges #" + node.outDegree());
71        for(EdgeCursor ec = node.outEdges(); ec.ok(); ec.next())
72        {
73          System.out.println(ec.edge());
74        }
75      }
76      
77        
78      //output the edges of the graph 
79      System.out.println("\nThe edges of the graph");
80      for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
81      {
82        System.out.println(ec.edge());
83      }
84  
85      //reverse edges that have consecutive neighbors in graph
86      //reversing means switching source and target node
87      for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
88      {
89        if(Math.abs(ec.edge().source().index() - ec.edge().target().index()) == 1) 
90          graph.reverseEdge(ec.edge());
91      }
92      
93      System.out.println("\nthe edges of the graph after some edge reversal");
94      for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
95      {
96        System.out.println(ec.edge());
97      }
98      
99      ///////////////////////////////////////////////////////////////////////////
100     // Node- and EdgeMap handling   ///////////////////////////////////////////
101     ///////////////////////////////////////////////////////////////////////////
102     
103     //create a nodemap for the graph
104     NodeMap nodeMap = graph.createNodeMap();
105     for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
106     {
107       //get node at current cursor position
108       Node node = nc.node();
109       //associate descriptive String to the node via a nodemap 
110       nodeMap.set(node,"this is node " + node.index());
111     }
112     
113     //create an edgemap for the graph
114     EdgeMap edgeMap = graph.createEdgeMap();
115     for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
116     {
117       //get edge at current cursor position
118       Edge edge = ec.edge();
119       //associate descriptive String to the edge via an edgemap
120       edgeMap.set(edge,"this is edge [" + 
121                   nodeMap.get(edge.source()) + "," + 
122                   nodeMap.get(edge.target()) + "]");
123     }
124     
125     //output the nodemap values of the nodes
126     System.out.println("\nThe node map values of the graph");
127     for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
128     {
129       System.out.println(nodeMap.get(nc.node()));
130     }
131     
132     //output the edges of the graph 
133     System.out.println("\nThe edge map values of the graph");
134     for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
135     {
136       System.out.println(edgeMap.get(ec.edge()));
137     }
138     
139     //cleanup unneeded node and edge maps again (free resources)
140     graph.disposeNodeMap(nodeMap);
141     graph.disposeEdgeMap(edgeMap);
142 
143     ///////////////////////////////////////////////////////////////////////////
144     // removing elements from the graph  //////////////////////////////////////
145     ///////////////////////////////////////////////////////////////////////////
146     
147     for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
148     {
149       //remove node that has a edge degree > 2
150       if(nc.node().degree() > 2)
151       {
152         //removed the node and all of its adjacent edges from the graph
153         graph.removeNode(nc.node());
154       }
155     }
156     System.out.println("\ngraph after some node removal");
157     System.out.println(graph);
158     
159     
160     
161   }
162   
163   public static void main(String args[])
164   {
165     new GraphDemo();
166   }
167 }
168