Skip to main content

Driver program to perform operations in graph

Driver program to perform operations in graph:-



package com.problems.graph;

public class GraphDriver {

 public static void main(String[] args)
 {
  BFSGraph g=new BFSGraph();
  g.addVertex('A');
  g.addVertex('B');
  g.addVertex('C');
  g.addVertex('D');
  g.addVertex('E');
  g.addVertex('F');
  g.addVertex('G');
  g.addVertex('H');
 
  g.addEdge(0, 1);
  g.addEdge(1, 2);
  g.addEdge(1, 7);
  g.addEdge(2, 3);
  g.addEdge(2, 4);
  g.addEdge(7, 4);
  g.addEdge(4, 5);
  g.addEdge(4, 6);
  g.bfs();
 }
 
}

Comments

.

Popular posts from this blog

Adding Node at the End of Linked List

Adding Node at the End of Linked List:- The method takes two parameter one a head node of a linked list and other data too insert. public void addNodeEnd ( Node head , int data ) { if ( head == null ) { System . out . println ( "list is empty" ); return ; } Node node = new Node ( data ); Node temp = head ; while ( temp . next != null ) { temp = temp . next ; } temp . next = node ; } To Know the basic structure of the linked list click here

DEPTH FIRST SEARCH IN JAVA

DEPTH FIRST SEARCH IN JAVA public void dfs () { vertex [ 0 ]. visited = true ; displyVertex ( 0 ); stackobj . push ( 0 ); while (! stackobj . isEmpty ()) { int v = adjVertex (( Integer ) stackobj . peek ()); if ( v ==- 1 ) { stackobj . pop (); } else { vertex [ v ]. visited = true ; displyVertex ( v ); stackobj . push ( v ); } } }