Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

implement graph in java example

class Graph {

  // inner class
  // to keep track of edges
  class Edge {
    int src, dest;
  }

  // number of vertices and edges
  int vertices, edges;

  // array to store all edges
  Edge[] edge;

  Graph(int vertices, int edges) {
    this.vertices = vertices;
    this.edges = edges;

    // initialize the edge array
    edge = new Edge[edges];
    for(int i = 0; i < edges; i++) {

      // each element of the edge array
      // is an object of Edge type
      edge[i] = new Edge();
    }
  }

  public static void main(String[] args) {

    // create an object of Graph class
    int noVertices = 5;
    int noEdges = 8;
    Graph g = new Graph(noVertices, noEdges);

    // create graph
    g.edge[0].src = 1;   // edge 1---2
    g.edge[0].dest = 2;

    g.edge[1].src = 1;   // edge 1---3
    g.edge[1].dest = 3;

    g.edge[2].src = 1;   // edge 1---4
    g.edge[2].dest = 4;

    g.edge[3].src = 2;   // edge 2---4
    g.edge[3].dest = 4;

    g.edge[4].src = 2;   // edge 2---5
    g.edge[4].dest = 5;

    g.edge[5].src = 3;   // edge 3---4
    g.edge[5].dest = 4;

    g.edge[6].src = 3;   // edge 3---5
    g.edge[6].dest = 5;

    g.edge[7].src = 4;   // edge 4---5
    g.edge[7].dest = 5;

    // print graph
    for(int i = 0; i < noEdges; i++) {
      System.out.println(g.edge[i].src + " - " + g.edge[i].dest);
    }

  }
}
Comment

java graph

List<Double> x = NumpyUtils.linspace(-3, 3, 100);
List<Double> y = x.stream().map(xi -> Math.sin(xi) + Math.random()).collect(Collectors.toList());

Plot plt = Plot.create();
plt.plot().add(x, y, "o").label("sin");
plt.legend().loc("upper right");
plt.title("scatter");
plt.show();
Comment

PREVIOUS NEXT
Code Example
Java :: Get the number of weeks between two dates 
Java :: java final method 
Java :: mp3 player java 
Java :: Java getClass() method 
Java :: java final class 
Java :: java printstacktrace 
Java :: java union type 
Java :: how to call child class method from parent class in java 
Java :: Spring boot init method 
Java :: how to find a int 
Java :: Java Access HashMap Elements 
Java :: what is java steam 
Java :: Java Scanner nextDouble() 
Java :: java forcing user to input int 
Java :: java scanner tokens with withespace 
Sql :: ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 
Sql :: How to select rows with no matching entry in another table? 
Sql :: create database mysql utf8 
Sql :: safe update mode in mysql 
Sql :: output oracle 
Sql :: postgresql blocked on mac 
Sql :: how to export database mysql terminal ubuntu 
Sql :: get all schemas postgres 
Sql :: sql add days to date 
Sql :: how to stop all connections to a psql 12 database? 
Sql :: mysql 1 day ago 
Sql :: truncate table mysql 
Sql :: get current month mysql 
Sql :: rror: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client 
Sql :: mysql take number in string 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =