集册 Java实例教程 基于表示使用jgrapht的链接结构的URL对象创建有向图

基于表示使用jgrapht的链接结构的URL对象创建有向图

欢马劈雪     最近更新时间:2020-01-02 10:19:05

792
基于表示使用jgrapht的链接结构的URL对象创建有向图

package org.blackbananacoin.premature;
/** 时代Java公众号 - N o w J a  v a . c o m 提 供 **/

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Set;


import org.jgrapht.DirectedGraph;

import org.jgrapht.graph.DefaultDirectedGraph;

import org.jgrapht.graph.DefaultEdge;


public class HelloJgraphT {


    public static void main(String[] args) {


        // create a graph based on URL objects
        /** 
         来自 NowJava.com - 时代Java**/

        DirectedGraph<URL, DefaultEdge> hrefGraph = createHrefGraph();


        // note directed edges are printed as: (<v1>,<v2>)

        System.out.println(hrefGraph.toString());


        for (URL url : hrefGraph.vertexSet()) {

            Set<DefaultEdge> edgesOutVertex = hrefGraph

                    .outgoingEdgesOf(url);

            System.out.println("[vertex]" + url + " -- out edge :"

                    + edgesOutVertex.size());

        }


    }


    /**

     * Creates a toy directed graph based on URL objects that represents link

     * structure.

     * 

     * @return a graph based on URL objects.

     */

    private static DirectedGraph<URL, DefaultEdge> createHrefGraph() {

        DirectedGraph<URL, DefaultEdge> g = new DefaultDirectedGraph<URL, DefaultEdge>(

                DefaultEdge.class);


        try {

            URL root = new URL("http://blackbananacoin.org");

            URL bkbcExchange = new URL(

                    "http://blackbananacoin.org/p/twd2btc/");

            URL bkbcFoo23 = new URL("http://blackbananacoin.org/p/foo23/");

            URL bkbcFoo23Haha24 = new URL(

                    "http://blackbananacoin.org/p/foo23/haha24");


            // add the vertices

            g.addVertex(root);

            g.addVe
展开阅读全文