KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > demo > HelloJGraphT


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -----------------
26  * HelloJGraphT.java
27  * -----------------
28  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
29  *
30  * Original Author: Barak Naveh
31  * Contributor(s): -
32  *
33  * $Id: HelloJGraphT.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 27-Jul-2003 : Initial revision (BN);
38  *
39  */

40 package org.jgrapht.demo;
41
42 import java.net.*;
43
44 import org.jgrapht.*;
45 import org.jgrapht.graph.*;
46
47
48 /**
49  * A simple introduction to using JGraphT.
50  *
51  * @author Barak Naveh
52  * @since Jul 27, 2003
53  */

54 public final class HelloJGraphT
55 {
56
57     //~ Constructors ----------------------------------------------------------
58

59     private HelloJGraphT()
60     {
61     } // ensure non-instantiability.
62

63     //~ Methods ---------------------------------------------------------------
64

65     /**
66      * The starting point for the demo.
67      *
68      * @param args ignored.
69      */

70     public static void main(String JavaDoc [] args)
71     {
72         UndirectedGraph<String JavaDoc, DefaultEdge> stringGraph = createStringGraph();
73
74         // note undirected edges are printed as: {<v1>,<v2>}
75
System.out.println(stringGraph.toString());
76
77         // create a graph based on URL objects
78
DirectedGraph<URL, DefaultEdge> hrefGraph = createHrefGraph();
79
80         // note directed edges are printed as: (<v1>,<v2>)
81
System.out.println(hrefGraph.toString());
82     }
83
84     /**
85      * Creates a toy directed graph based on URL objects that represents link
86      * structure.
87      *
88      * @return a graph based on URL objects.
89      */

90     private static DirectedGraph<URL, DefaultEdge> createHrefGraph()
91     {
92         DirectedGraph<URL, DefaultEdge> g =
93             new DefaultDirectedGraph<URL, DefaultEdge>(DefaultEdge.class);
94
95         try {
96             URL amazon = new URL("http://www.amazon.com");
97             URL yahoo = new URL("http://www.yahoo.com");
98             URL ebay = new URL("http://www.ebay.com");
99
100             // add the vertices
101
g.addVertex(amazon);
102             g.addVertex(yahoo);
103             g.addVertex(ebay);
104
105             // add edges to create linking structure
106
g.addEdge(yahoo, amazon);
107             g.addEdge(yahoo, ebay);
108         } catch (MalformedURLException e) {
109             e.printStackTrace();
110         }
111
112         return g;
113     }
114
115     /**
116      * Craete a toy graph based on String objects.
117      *
118      * @return a graph based on String objects.
119      */

120     private static UndirectedGraph<String JavaDoc, DefaultEdge> createStringGraph()
121     {
122         UndirectedGraph<String JavaDoc, DefaultEdge> g =
123             new SimpleGraph<String JavaDoc, DefaultEdge>(DefaultEdge.class);
124
125         String JavaDoc v1 = "v1";
126         String JavaDoc v2 = "v2";
127         String JavaDoc v3 = "v3";
128         String JavaDoc v4 = "v4";
129
130         // add the vertices
131
g.addVertex(v1);
132         g.addVertex(v2);
133         g.addVertex(v3);
134         g.addVertex(v4);
135
136         // add edges to create a circuit
137
g.addEdge(v1, v2);
138         g.addEdge(v2, v3);
139         g.addEdge(v3, v4);
140         g.addEdge(v4, v1);
141
142         return g;
143     }
144 }
145
Popular Tags