JAVA Implementation of Common graph algorithms

Source: Internet
Author: User
/*** <P> title: Graph traversal, Minimum Spanning Tree, and shortest path </P> *** <p> description: ** uses the adjacent matrix as the graph storage structure and has the right to have an undirected graph. The unconnected value is-1. ** in the graph traversal, the deep Traversal method is recursive, breadth traversal using secondary queue ** the Minimum Spanning Tree uses Kruskal) Algorithm Use an array to record node connectivity ** the Shortest Path of the graph adopts the dijela (Dijkstra) algorithm and uses the path through which queue records are recorded in turn ** modify: * In depth and breadth traversal, when the adjacent nodes of the nth node are listed and one-to-one stack/team is added, adjacent nearest priority stack/team ** </P> ** <p> Website: www.hartech.cn </P> * <p> page: http://www.hartech.cn/blog/blogview.asp? Logid = 88 </P> ** <p> date: 2006-09-08 </P> */public class graph {// graph ing matrix Private Static int [] [] arcs; // number of nodes Private Static int num; // record whether Private Static Boolean [] hasvisit has been accessed; // record the previous node that has been accessed, used to count the total length of a line static int pre; // depth-first traversal. The public static void traverse_dfs (INT [] [] arcs_in, int begin) of the graph neighbor matrix and nodes that start traversal are provided) {pre = begin; If (arcs_in = NULL | arcs_in.length = 0 | arcs_in.length! = Arcs_in [0]. Length | begin <0) {system. Err. println ("wrong arcs [] [] or begin! "); Return;} arcs = arcs_in; num = arcs. length; hasvisit = new Boolean [num]; DFS (BEGIN);} Private Static void DFS (INT begin) {hasvisit [begin] = true; Main. q_dfs.enqueue (BEGIN); main. length_dfs + = Main. ARCs [pre] [begin]; Pre = begin; int min, n = 0; For (INT I = 1; I <num; I ++) {// min = integer for the shortest distance to the first stack. max_value; For (Int J = 0; j <num; j ++) {If (! Hasvisit [J] & arcs [begin] [J]! =-1 & arcs [begin] [J] <min) {min = arcs [begin] [J]; n = J ;}} if (min = integer. max_value) {break;} else {DFS (n) ;}}// span traversal is preferred, public static void traverse_bfs (INT [] [] arcs_in, int begin) {pre = begin; if (arcs_in = NULL | arcs_in.length = 0 | arcs_in.length! = Arcs_in [0]. Length | begin <0) {system. Err. println ("wrong arcs [] [] or begin! "); Return;} arcs = arcs_in; num = arcs. length; hasvisit = new Boolean [num]; queue = new Queue (); hasvisit [begin] = true; queue. enqueue (BEGIN); int temp, Min, n = 0; while (! Queue. isempty () {temp = (integer) queue. dequeue ()). intvalue (); For (INT I = 1; I <num; I ++) {// min = integer in the nearest position. max_value; For (Int J = 0; j <num; j ++) {If (! Hasvisit [J] & arcs [temp] [J]! =-1 & arcs [temp] [J] <min) {min = arcs [temp] [J]; n = J ;}} if (min = integer. max_value) {break;} else {hasvisit [N] = true; queue. enqueue (n); main. q_bfs.enqueue (n); main. length_bfs + = Main. ARCs [pre] [N]; Pre = n ;}}// construct the minimum spanning tree using Kruskal) algorithm // use an array to record the connectivity of nodes. Public static void minispantree (INT [] [] arcs_in) {If (arcs_in = NULL | arcs_in.length = 0 | arcs_in.length! = Arcs_in [0]. Length) {system. Err. println ("wrong arcs [] []! "); Return;} arcs = arcs_in; num = arcs. length; // use an array to record node connectivity. // For example, group [0] = 0 indicates that the node is not connected to any node. // group [0] = group [3] = group [5] = 2 indicates that nodes 0, 3, and 5 are in the same connected graph, the ID of the connected graph is 2 int [] group = new int [num]; Boolean finish = false; int temp = 0, Min, n1 = 0, n2 = 0, groupnum = 0; // when all group [N] is equal to the same value, that is, the while (! Finish) {// traverse all paths, undirected graphs, only the upper triangle of the matrix. // find the shortest and not in the same connected graph (group [N1]! = Group [n2]), or the two nodes are not added to the connected graph min = integer. max_value; For (INT I = 0; I <num; I ++) {for (Int J = I + 1; j <num; j ++) {If (ARCs [I] [J] <min & arcs [I] [J]! =-1) {If (group [I]! = Group [J] | (group [I] = 0 & group [J] = 0) {min = arcs [I] [J]; n1 = I; N2 = J ;}}// if (min = integer. max_value) {break;} Main. q_tree.enqueue (new dimension (N1, N2); main. length_tree + = Main. ARCs [N1] [n2]; // join the connected graph group if (group [N1] = 0 & group [n2] = 0) {groupnum ++; group [N1] = groupnum; group [n2] = groupnum;} else if (group [N1]! = 0 & group [n2]! = 0) {temp = group [n2]; for (INT I = 0; I <num; I ++) {If (group [I] = temp) {group [I] = group [N1] ;}} else {If (group [N1] == 0) {group [N1] = group [n2];} else {group [n2] = group [N1] ;}// all groups [N] are equal to the same value and not 0, that is, the connected graph ends temp = 1; while (temp <num & group [temp] = group [0]) {temp ++ ;} if (group [0]! = 0 & temp = num) {finish = true ;}} if (! Finish) {system. out. println ("Graph non-Strongly Connected Graph") ;}// the Shortest Path of the graph. The vertex, start point, and end point are given, print the passing node // use the dijela (Dijkstra) algorithm public static void shortestpath_dij (INT [] [] arcs_in, int begin, int end) {If (arcs_in = NULL | arcs_in.length = 0 | arcs_in.length! = Arcs_in [0]. Length) {system. Err. println ("wrong arcs [] []! "); Return;} arcs = arcs_in; num = arcs. length; // indicates whether the shortest path has been found for the node. From begin to N, the value is finish [N] = true; Boolean [] Finish = new Boolean [num]; // The shortest path from begin to N is Min [N] int [] d = new int [num]. // use the queue record path to pass through the node queue [] queue = new queue [num]; // initialize for (INT I = 0; I <num; I ++) {d [I] = arcs [begin] [I]; finish [I] = false; queue [I] = new Queue ();} finish [begin] = true; d [begin] =-1; int V = 0, min = 0; // One loop finds the shortest distance (num-1 in total) for (INT I = 1; I <num; I ++) {min = integer. max_value; // scan to find the smallest d [] for (int w = 0; W <num; W ++) {If (! Finish [w] & D [w] <min & D [w]! =-1) {v = W; min = d [w] ;}} finish [v] = true; // The target is found and the loop is exited if (V = end) {queue [v]. enqueue (V); main. q_shortest = queue [v]. clone (); main. length_short = d [v]; break;} // update data of each d [] for (int w = 0; W <num; W ++) {If (! Finish [w] & arcs [v] [W]! =-1) {If (ARCs [v] [W] + min) <D [w] | D [w] =-1) {d [w] = arcs [v] [W] + min; queue [w] = queue [v]. clone (); queue [w]. enqueue (v) ;}} queue [v]. enqueue (v );}}}

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.