In-depth traversal of [algorithms] graphs (adjacent matrices)

Source: Internet
Author: User

Package COM. feelang; <br/> Import Java. util. vertex; <br/> class arcnode {<br/> int adjvex; <br/> arcnode next; <br/>}< br/> class vertexnode {<br/> char vertex; <br/> arcnode firstedge; <br/>}< br/> public class graph {<br/> Public static void main (string [] ARGs) {<br/> int n = 3; <br/> int e = 3; <br/> char [] vertexs = new char [N]; <br/> vertexs [0] = 'a '; <br/> vertexs [1] = 'B'; <br/> vertexs [2] = 'C'; </P> <p> G Raph algraph = New Graph (vertexs, N, e); <br/> algraph. DFS (); <br/>}</P> <p> Public graph (char [] vertexs, int node, int edge) {<br/> This. node = node; <br/> This. edge = edge; </P> <p> adjlist = new vertexnode [this. node]; <br/> visited = new Boolean [this. node]; <br/> for (INT I = 0; I <this. node; I ++) {<br/> adjlist [I] = new vertexnode (); <br/> adjlist [I]. vertex = vertexs [I]; <br/> adjlist [I]. firstedge = NULL; <Br/>}</P> <p> limit SC = new limit (system. in); <br/> for (INT I = 0; I <this. edge; I ++) {<br/> int V, U; <br/> V = SC. nextint (); <br/> U = SC. nextint (); </P> <p> If (V> = This. node | u> = This. node) {<br/> system. out. println ("wrong input"); <br/> I --; <br/> continue; <br/>}< br/> // use the forward Insertion Method for memory saving <br/> arcnode S = new arcnode (); <br/> S. adjvex = u; <br/> S. next = adjlist [v]. firstedge; <br/> adjlist [v]. firstedg E = s; <br/>}</P> <p> // output: visited [u] is set to true for all nodes U reachable from v <br/> Public void authentication E (INT v) {<br/> visited [v] = true; <br/> system. out. println (V); <br/> arcnode an = adjlist [v]. firstedge; <br/> while (! = NULL) {<br/> If (! Visited [v]) {<br/> E (. adjvex); <br/>}< br/> An =. next; <br/>}</P> <p> Public void DFS () {</P> <p> for (INT I = 0; I <this. node; I ++) {<br/> If (! Visited [I]) {<br/> caffe (I ); <br/>}</P> <p> private vertexnode [] adjlist; <br/> private Boolean [] visited; <br/> private int node; <br/> private int edge; <br/>}< br/>

Note: line28 and line31, Java array space allocation issues.

Algorithm reference: Algorithm Analysis and Design

Related Article

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.