简体   繁体   中英

Drawing ring topology in console for chord algorithm C++

I am working on an implementation of the P2P DHT chord algorithm in C++. I want to implement a feature where the user can view the ring.network topology with the participating nodes in it. Something similar to below.

在此处输入图像描述

The computer icons in the picture are not necessary. However, I envision the topology to also give details about the individual nodes - the respective IP addresses, IDs etc.

I just want a very simple visual representation for the user to see the number of nodes involved at a given point of time. Which tool / library can help me with this?

Java code for the Ring Topology

import java.util.*;

class RingTopology {

    static void addEdge(Vector<Integer> adj[],
                        int u, int v)
    {
        adj[u].add(v);
        adj[v].add(u);
    }

    static void printTopology(Vector<Integer> adj[], int V)
    {
        for(int v = 0; v < V; ++v)
        {
            System.out.print("\n Adjacency list of vertex " +
                    v + "\n head ");
            for(int x : adj[v])
                System.out.print(". " + x);

            System.out.printf("\n");
        }
    }

    static boolean checkRingTopology(Vector<Integer> adj[],
                                     int V, int E)
    {

        // Number of edges should be equal
        // to Number of vertices
        if (E != V)
            return false;

        // For a graph to represent a ring
        // topology should have greater
        // than 2 nodes
        if (V <= 2)
            return false;

        int[] vertexDegree = new int[V + 1];

        // Calculate the degree of each vertex
        for(int i = 1; i <= V; i++)
        {
            for(int v : adj[i])
            {
                vertexDegree[v]++;
            }
        }

        // countDegree2 stores the count of
        // the vertices having degree 2
        int countDegree2 = 0;

        for(int i = 1; i <= V; i++)
        {
            if (vertexDegree[i] == 2)
            {
                countDegree2++;
            }
        }

        // If all three necessary conditions
        // as discussed, satisfy return true
        if (countDegree2 == V)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Function to check if the graph represents
// a Ring topology
    static void checkRing(Vector<Integer> adj[], int V, int E)
    {
        boolean isRing = checkRingTopology(adj, V, E);
        if (isRing)
        {
            System.out.print("YES" + "\n");
        }
        else
        {
            System.out.print("NO" + "\n");
        }
    }
    
    public static void main(String[] args) {
        
        int V = 6, E = 6;

        Vector<Integer>[] adj1 = new Vector[V + 1];
        for (int i = 0; i < adj1.length; i++)
            adj1[i] = new Vector<Integer>();

        addEdge(adj1, 1, 2);
        addEdge(adj1, 2, 3);
        addEdge(adj1, 3, 4);
        addEdge(adj1, 4, 5);
        addEdge(adj1, 6, 1);
        addEdge(adj1, 5, 6);

        checkRing(adj1, V, E);
    
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM