写了一个SetMutableGraph,主要是在ArrayListMutableGraph的基础上用IntAVLTreeSet自动排序,效率应该不错。代码如下: 欢迎搞web数据挖掘的同学一起探讨研究:

package edu.dut.wisdom;/*
* Copyright (C) 2006-2007 Sebastiano Vigna
*
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the Free
*  Software Foundation; either version 2 of the License, or (at your option)
*  any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
*  for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/import it.unimi.dsi.fastutil.ints.IntAVLTreeSet;
import it.unimi.dsi.fastutil.ints.IntBidirectionalIterator;
import it.unimi.dsi.fastutil.ints.IntIterator;
import it.unimi.dsi.fastutil.objects.ObjectArrays;
import it.unimi.dsi.lang.MutableString;
import it.unimi.dsi.webgraph.ImmutableGraph;
import it.unimi.dsi.webgraph.LazyIntIterator;
import it.unimi.dsi.webgraph.LazyIntIterators;
import it.unimi.dsi.webgraph.NodeIterator;
import it.unimi.dsi.webgraph.Transform;public class SetMutableGraph extends ImmutableGraph {
/** Current number of nodes. */
protected int n;
/** Current number of arcs. */
protected long m;
/** Current list of successor lists. The backing array might be longer than {@link #n}. */
protected IntAVLTreeSet successors[];private final static IntAVLTreeSet[] EMPTY_INTARRAYLIST_ARRAY = {};/** Guarantees that a node index is valid.
*
* @param x a node index.
*/
protected void ensureNode( final int x ) {
if ( x < 0 ) throw new IllegalArgumentException( "Illegal node index " + x );
if ( x >= n ) throw new IllegalArgumentException( "Node index " + x + " is larger than graph order (" + n + ")" );
}/** Creates a new empty mutable graph. */
public SetMutableGraph() {
successors = EMPTY_INTARRAYLIST_ARRAY;
}/** Creates a new mutable graph using a given number of nodes and a given list of arcs.
*
* @param numNodes the number of nodes in the graph.
* @param arc an array of arrays of length 2, specifying the arcs; no sanity checks are performed..
*/
public SetMutableGraph( final int numNodes, final int[][] arc ) {
n = numNodes;
m = arc.length;// Sanitize
for( int i = arc.length; i-- != 0; ) {
if ( arc[ i ].length != 2 ) throw new IllegalArgumentException( "The arc of index " + i + " has length " + arc[ i ].length );
if ( arc[ i ][ 0 ] < 0 || arc[ i ][ 1 ] < 0 || arc[ i ][ 0 ] >= numNodes || arc[ i ][ 1 ] >= numNodes ) throw new IllegalArgumentException( "The arc of index " + i + " (" + arc[ i ][ 0 ] + ", " + arc[ i ][ 1 ] + ") is illegal" );
}
successors = new IntAVLTreeSet[ n ];
for( int i = n; i-- != 0; ) successors[ i ] = new IntAVLTreeSet();
for( int i = 0; i < arc.length; i++ ) successors[ arc[ i ][ 0 ] ].add( arc[ i ][ 1 ] );
}/** Creates a new mutable graph copying a given immutable graph.
*
* @param g an immutable graph.
*/
public SetMutableGraph( final ImmutableGraph g ) {
successors = new IntAVLTreeSet[ 0 ];
int d, s = -1;
long numArcs = 0;
for( NodeIterator nodeIterator = g.nodeIterator(); nodeIterator.hasNext(); ) {
s = nodeIterator.nextInt();
d = nodeIterator.outdegree();
numArcs += d;
successors = ObjectArrays.grow( successors, s + 1 );
successors[ s ] = new IntAVLTreeSet( nodeIterator.successorArray(), 0, d );
}
n = s + 1;
m = numArcs;
}/** Creates a new mutable graph using a given number of nodes and a given arc filter.
*
* @param numNodes the number of nodes in the graph.
* @param arcFilter an arc filter which will specify which arcs go into the graph.
*/
public SetMutableGraph( final int numNodes, final Transform.ArcFilter arcFilter ) {
n = numNodes;
successors = new IntAVLTreeSet[ n ];
for( int i = n; i-- != 0; ) {
successors[ i ] = new IntAVLTreeSet();
for( int j = 0; j < n; j++ )
if ( arcFilter.accept( i, j ) ) {
successors[ i ].add( j );
m++;
}
}
}/** Returns a new mutable graph containing a directed cycle.
*
* @param numNodes the number of nodes in the cycle.
*/
public static SetMutableGraph newDirectedCycle( final int numNodes ) {
return new SetMutableGraph( numNodes, new Transform.ArcFilter() {
public boolean accept( final int i, final int j ) {
return ( i + 1 ) % numNodes == j;
}
});
}/** Returns a new mutable graph containing a bidirectional cycle.
*
* @param numNodes the number of nodes in the cycle.
*/
public static SetMutableGraph newBidirectionalCycle( final int numNodes ) {
return new SetMutableGraph( numNodes, new Transform.ArcFilter() {
public boolean accept( final int i, final int j ) {
return ( i + 1 ) % numNodes == j || ( j + 1 ) % numNodes == i;
}
});
}/** Returns a new mutable graph containing a complete graph.
*
* @param numNodes the number of nodes in the graph.
* @param loops true if you want loops, too.
*/
public static SetMutableGraph newCompleteGraph( final int numNodes, final boolean loops ) {
return new SetMutableGraph( numNodes, new Transform.ArcFilter() {
public boolean accept( final int i, final int j ) {
return i != j || loops;
}
});
}/** Returns a new mutable graph containing a complete binary in-tree of given height.
*
* Warning: starting from version 1.7, the spurious loop
* at the root has been removed.
*
* @param height the height of the tree (0 for the root only).
*/
public static SetMutableGraph newCompleteBinaryIntree( final int height ) {
return new SetMutableGraph( ( 1 << ( height + 1 ) ) - 1, new Transform.ArcFilter() {
public boolean accept( final int i, final int j ) {
return i != j && i / 2 == j;
}
});
}/** Returns a new mutable graph containing a complete binary out-tree of given height.
*
* Warning: starting from version 1.7, the spurious loop
* at the root has been removed.
*
* @param height the height of the tree (0 for the root only).
*/
public static SetMutableGraph newCompleteBinaryOuttree( final int height ) {
return new SetMutableGraph( ( 1 << ( height + 1 ) ) - 1, new Transform.ArcFilter() {
public boolean accept( final int i, final int j ) {
return i != j && j / 2 == i;
}
});
}@Override
public int numNodes() {
return n;
}@Override
public int outdegree( final int x ) {
ensureNode( x );
return successors[ x ].size();
}@Override
public long numArcs() {
return m;
}@Override
public int[] successorArray( final int x ) {
ensureNode( x );
return successors[ x ].toIntArray();
}@Override
public LazyIntIterator successors( final int x ) {
ensureNode( x );
return LazyIntIterators.lazy(successors[x].iterator());
}/** Adds the given number of nodes, numbering them from {@link #numNodes()} onwards. The new nodes have no successors.
*
* @param numNewNodes the number of new nodes.
*/
public void addNodes( final int numNewNodes ) {
if ( numNewNodes != 0 ) {
successors = ObjectArrays.ensureCapacity( successors, n + numNewNodes, n );
while( n < numNewNodes ) successors[ n++ ] = new IntAVLTreeSet();
}
}/** Removes the given node. All arcs incident on the node are removed, too.
*
* @param x the node to be removed.
*/
public void removeNode( final int x ) {
ensureNode( x );
System.arraycopy( successors, x + 1, successors, x, --n - x );
int t;
for( int i = n; i-- != 0; ) {
IntBidirectionalIterator it = successors[i].iterator();
while(it.hasNext()) {
t = it.nextInt();
if ( t == x)
it.remove();
else if (t > x) {
successors[i].remove(t);
successors[i].add(t - 1);
}
}
}
}/** Adds the given arc.
*
* @param x the start of the arc.
* @param y the end of the arc.
*/
public void addArc( final int x, final int y ) {
ensureNode( x );
ensureNode( y );
if (successors[ x ].contains(y))
return;
successors[ x ].add( y );
m++;
}/** Removes the given arc.
*
* @param x the start of the arc.
* @param y the end of the arc.
*/
public void removeArc( final int x, final int y ) {
ensureNode( x );
ensureNode( y );
if ( !successors[ x ].contains(y) )
return;
successors[ x ].remove(y);
m--;
}/** Compare this mutable graph to another object.
*
* @return true iff the given object is a mutable graph the same size, and
* the successor list of every node of this graph is equal to the successor list of the corresponding node of o.
*/@Override
public boolean equals( final Object o ) {
if ( ! (o instanceof SetMutableGraph ) ) return false;
final SetMutableGraph g = (SetMutableGraph ) o;
int n = numNodes();
if ( n != g.numNodes() ) return false;
int[] s, t;
int d;
while( n-- != 0 ) {
if ( ( d = outdegree( n ) ) != g.outdegree( n ) ) return false;
s = successorArray( n );
t = g.successorArray( n );
while( d-- != 0 ) if ( s[ d ] != t[ d ] ) return false;
}return true;
}/** Returns a hash code for this mutable graph.
*
* @return a hash code for this mutable graph.
*/@Override
public int hashCode() {
int n = numNodes(), h = -1;
int[] s;
int d;
for( int i = 0; i < n; i++ ) {
h = h * 31 + i;
s = successorArray( i );
d = outdegree( i );
while( d-- != 0 ) h = h * 31 + s[ d ];
}return h;
}@Override
public String toString() {
MutableString ms = new MutableString();
IntIterator ii;ms.append( "Nodes: " + numNodes() + "\nArcs: " + numArcs() + "\n" );
for ( int i = 0; i < numNodes(); i++ ) {
ms.append( "Successors of " + i + " (degree " + outdegree( i ) + "):" );
ii = successors[i].iterator();
while ( ii.hasNext() )
ms.append( " " + ii.nextInt() );
ms.append( "\n" );
}
return ms.toString();
}@Override
public ImmutableGraph copy() {
return this;
}@Override
public boolean randomAccess() {
return true;
}
}

测试代码:

package edu.dut.wisdom;
import java.io.IOException;import it.unimi.dsi.webgraph.ASCIIGraph;public class Main {public static void main(String[] args) throws IOException {SetMutableGraph sg = new SetMutableGraph(7, new int[][] {});sg.addArc(0, 6);sg.addArc(0, 4);sg.addArc(1, 5);sg.addArc(1, 3);sg.addArc(2, 1);sg.addArc(3, 6);sg.addArc(4, 5);sg.addArc(5, 6);sg.addArc(6, 2);sg.removeNode(2);ASCIIGraph.store(sg, "c:\\d");}}

转载于:https://www.cnblogs.com/youwang/archive/2009/12/29/2310647.html

最新文章

  1. 关于ubuntu 14.04 dpkg 问题
  2. leetcode - 264. 丑数 II
  3. Java中的IO流(六)
  4. Dubbo思维导图知识点整理
  5. Mysql学习总结(32)——MySQL分页技术详解
  6. Eclipse集成lombok插件 解决@Data标签get/set方法找不到异常
  7. 小米php架构图,小米商城基本框架部分
  8. 第二十期 在Android中修改GPS定位数据的完整方案《手机就是开发板》
  9. EndnoteX7插入文献时,提示“访问未命名的文件时尝试越过其结尾”的解决方法
  10. 【高数】Abel定理,幂级数的和收敛半径,不同幂级数收敛半径的比较,缺项幂级数的解法
  11. 小暑将至,这组小暑海报送给你们
  12. 考研政治之马克思主义原理篇
  13. mysql----where 1=1是什么意思
  14. 【Flutter实战】六大布局组件及半圆菜单案例
  15. MTK 6761平台 android O bootloader启动之 Pre-loader - Lk
  16. 【思维题】CodeForce 817A Treasure Hunt
  17. 查看oracle操作日志
  18. 二项分布均值,方差推导
  19. Asp.net夜话之二:asp.net内置对象
  20. 带中继节点的蜂窝小区通信系统matlab仿真,输出吞吐量,功耗以及能效等

热门文章

  1. 我以为内卷是外包的反义词!
  2. 你一直在用的Beam Search,是否真的有效?
  3. 施一公:论文和科技实力是两回事,大家千万要分开
  4. 【情感分析】基于Aspect的情感分析模型总结(一)
  5. 具体数学-第6课(下降阶乘幂)
  6. leetcode—14.链表反转类题目python解答
  7. 所以,我们依然要读书
  8. 程序员面试揭秘之求职渠道
  9. 人脸检测-Haar分类器方法
  10. 服务器硬件工程师从入门到精通_硬件工程师必阅--如何“0基础”从入门到精通?(附CAD使用指南)...