+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2008 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// GraphHelper.java\r
-// Since: Oct 24, 2008 11:03:03 AM\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/graph/GraphHelper.java $\r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.util.graph;\r
-\r
-/**\r
- * A helper class to implement graph related methods\r
- * \r
- * package scope\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class GraphHelper\r
-{\r
- /**\r
- * Prohibit construction\r
- */\r
- private GraphHelper()\r
- {};\r
-\r
- public static <NodeLabel, EdgeLabel> NodeLabel getSourceNodeLabel(Graph<NodeLabel, EdgeLabel> graph, Edge edge)\r
- {\r
- return graph.getNodeLabel(edge.getSourceNodeID());\r
- }\r
-\r
- public static <NodeLabel, EdgeLabel> NodeLabel getDestNodeLabel(Graph<NodeLabel, EdgeLabel> graph, Edge edge)\r
- {\r
- return graph.getNodeLabel(edge.getDestNodeID());\r
- }\r
-\r
- public static <NodeLabel, EdgeLabel> String toString(Graph<NodeLabel, EdgeLabel> graph, Edge edge)\r
- {\r
- return String.format("(%s,%s)", graph.getNodeLabel(edge.getSourceNodeID()).toString(), graph.getNodeLabel(\r
- edge.getDestNodeID()).toString());\r
- }\r
-\r
- /**\r
- * compute the transitive closure of the input graph\r
- * \r
- * @param <NodeLabel>\r
- * @param <EdgeLabel>\r
- * @param original\r
- * @return\r
- */\r
- public static <NodeLabel, EdgeLabel> Graph<NodeLabel, EdgeLabel> transitiveClosure(Graph<NodeLabel, EdgeLabel> input)\r
- {\r
- Graph<NodeLabel, EdgeLabel> graph = AdjacencyList.copy(input);\r
-\r
- int numChange = 0;\r
- do\r
- {\r
- numChange = 0;\r
- for (NodeLabel from : graph.getNodeLabelSet())\r
- {\r
- for (NodeLabel to : graph.getNodeLabelSet())\r
- {\r
- if (graph.hasEdge(from, to))\r
- continue;\r
-\r
- int destNodeID = graph.getNodeID(to);\r
- for (Edge startPoint : graph.getOutEdgeSet(from))\r
- {\r
- int intermediateNodeID = startPoint.getDestNodeID();\r
-\r
- if (graph.hasEdge(new Edge(intermediateNodeID, destNodeID)))\r
- {\r
- graph.addEdge(from, to);\r
- ++numChange;\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- while (numChange > 0);\r
-\r
- return graph;\r
- }\r
-\r
-}\r