Graph theory adjacency linked list storage BFS DFS topology ordering

Source: Internet
Author: User



Package algorithms;

Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.LinkedList;
Import Java.util.Stack;

public class Graphic {
public static Class vertex{
public int num;//Node number
The weight of the public int weight;//Edge
Public Vertex next;//points to the next arc end of the vertex, which is the next edge
Public Vertex (int num,int Weight,vertex next)
{
This.num=num;
This.weight=weight;
This.next=next;
}
}
The number of vertices of the private int size;//graph
Private Boolean isdirection;//is a forward graph true,
Private Boolean visit[];
Private Vertex p[];//Stores the precursors of each vertex
private int distance[];//Breadth precedence over time to store the distance from the root node
Private Vertex adj[];
public int startt[];//The start time of the depth-first traversal
public int endt[];//Depth-first traversal end time for topological ordering
int time;//Depth Precedence elapsed timer that records the start and end times of each node
Linkedlist<vertex>topologicalsort=new linkedlist<vertex> ();//array that holds the topological sort


Public Graphic (int vernum,boolean isdirection) {
Adj=new Vertex[vernum];
P=new Vertex[vernum];
Size=vernum;
This.isdirection=isdirection;
Visit=new Boolean[vernum];
distance=new int [vernum];
endt=new int [size];
startt=new int [size];
Constructs vertices to prevent vertices in the following forward graph from being arcs, so that the vertex array is a null pointer
for (int i=0;i<size;i++)
{
Adj[i]=new Vertex (i, 0, NULL);
}
}
/**
* Create diagram Insert all nodes to create Diagram connection table representation
* x y indicates arc head, and ARC tail respectively
*/
public void Inserte (int x,int y)
{
Vertex Temp=adj[x];
while (Temp.next!=null)
Temp=temp.next;
Temp.next=new Vertex (y, 0, NULL);
if (Isdirection==false)
{
if (adj[y]==null)
Adj[y]=new Vertex (y, 0, NULL);
Vertex Temp1=adj[y];
while (Temp1.next!=null)
Temp1=temp1.next;
Temp1.next=new Vertex (x, 0, NULL);
}
}
/** Breadth-First traversal
* @param x: The root node of the graph begins to traverse
* The nodes that are stored in the queue are going to be accessed so set to true to avoid repeated additions
*/
public void BFS (int x)
{
Initialization
Arrays.fill (visit, false);
Arrays.fill (distance,-1);
Arrays.fill (P, NULL);
Java.util.queue<integer> q=new linkedlist<integer> ();
distance[x]=0;//initialize the depth from the root node
Visit[x]=true;
Q.add (x);
while (!q.isempty ())
{
This iterates through the edges of the vertices, to find the reference in the fixed-point array to find the corresponding adjacency table, otherwise
Only the edges of one vertex are turned around, so the number of vertices used in the queue means not to use references
Using references, you also find references on the corresponding fixed-point array
Vertex Parent=adj[q.poll ()];
System.out.print (parent.num+ "");
Vertex Temp=parent.next;
while (Temp!=null)//Assign a value to the predecessor and depth of this node when adding a node to the queue
{
if (Visit[temp.num]==false)
{
P[temp.num]=parent;
visit[temp.num]=true;//Add an Access flag after entering the queue to add a flag to prevent
Repeat access repeated
distance[temp.num]=distance[p[temp.num].num]+1;
Q.add (Temp.num);
}
Temp=temp.next;
}
}
}
/**
* Depth-first traversal and topology sequencing
*/
public void DFS ()
{
Initialization
Arrays.fill (visit, false);
Arrays.fill (distance,-1);
Arrays.fill (P, NULL);
Arrays.fill (Endt, 0);
Arrays.fill (Startt, 0);
time=0;
for (int i=0;i<size;i++)//Avoid some graphs that are not strong connected
{
if (visit[i]==false)//Note This condition cannot be written to the for loop because it encounters 2
The exit loop that has been accessed is no longer iterative and should be placed in the loop body
DFSVISIT3 (i);
}
}
Recursive implementation
public void dfsvisit (int i)
{
time++;//time to start a visit
Startt[i]=time;
Visit[i]=true;
System.out.print (i+ "");
Vertex Temp=adj[i].next;
while (Temp!=null)
{
if (Visit[temp.num]==false)
Dfsvisit (Temp.num);
Temp=temp.next;
}
time++;//Access completed +1, the time of the recording end is the same as the start time if not added
Endt[i]=time;
Topologicalsort.addfirst (Adj[i]);
}
Using the Stack implementation
public void DFSvisit2 (int i)
{
Stack<integer> s=new stack<integer> ();
S.push (i);
while (!s.isempty ())
{
time++;
int Num=s.pop ();
Visit[num]=true;
System.out.print (num+ "");
Vertex Temp=adj[num].next;
while (Temp!=null)
{
if (Visit[temp.num]==false)
S.push (Temp.num);
Temp=temp.next;
}
}
}
Using stacks for topological sequencing
public void DFSvisit3 (int i)
{
Stack<integer> s=new stack<integer> ();
S.push (i);
while (!s.isempty ())
{
time++;
int Num=s.peek ();
if (Visit[num]==false)
{
Visit[num]=true;
Startt[num]=time;
System.out.print (num+ "");
Vertex Temp=adj[num].next;
while (Temp!=null)
{
if (Visit[temp.num]==false)
S.push (Temp.num);
Temp=temp.next;
}
}
Else
{
Endt[num]=time;
S.pop ();
Topologicalsort.addfirst (Adj[num]);
}
}
}
/**
* Topological sequencing (for loop-free graphs)
* Depth-first traversal by end time from large to small sort
* End time large represented as the front node
*/
public void Topologicalsort ()
{
DFS ();
for (int i=0;i<topologicalsort.size (); i++)
{
int Num=topologicalsort.get (i). Num;
System.out.println ("+i+" executed by "+" Node "+num+": "+startt[num]+"/"+endt[num]");
}
}
public static void Main (String []args)
{
Graphic g=new Graphic (9, true);
G.inserte (0, 1);
G.inserte (0, 6);
G.inserte (1, 6);
G.inserte (1, 2);
G.inserte (7, 6);
G.inserte (5, 2);
G.inserte (5, 4);
G.inserte (4, 3);
G.inserte (2, 3);
G.inserte (8, 8);
G.dfs ();
G.topologicalsort ();
}
}

Graph theory adjacency linked list storage BFS DFS topology ordering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.