文章目录 一、引入 二、基本概念 三、图的表示 四、图的遍历 4.1 图的深度优先遍历(DFS) 4.2 图的广度优先遍历(BFS) 4.3 图的深度优先 VS 广度优先
一、引入
二、基本概念
三、图的表示
package com. gyh. grapg ; import java. util. ArrayList ;
import java. util. Arrays ;
public class Graph { private ArrayList < String > vertexList; private int [ ] [ ] edges; private int numOfEdges = 0 ; public static void main ( String [ ] args) { int n = 5 ; String [ ] VertexValue = { "A" , "B" , "C" , "D" , "E" } ; Graph graph = new Graph ( 5 ) ; for ( String s : VertexValue ) { graph. insertVertex ( s) ; } graph. insertEdge ( 0 , 1 , 1 ) ; graph. insertEdge ( 0 , 2 , 1 ) ; graph. insertEdge ( 1 , 2 , 1 ) ; graph. insertEdge ( 1 , 3 , 1 ) ; graph. insertEdge ( 1 , 4 , 1 ) ; graph. showGraph ( ) ; } public Graph ( int n) { this . edges = new int [ n] [ n] ; vertexList = new ArrayList < > ( ) ; } public int getNumOfVertex ( ) { return vertexList. size ( ) ; } public int getNumOfEdges ( ) { return numOfEdges; } public void showGraph ( ) { for ( int [ ] edge : edges) { System . out. println ( Arrays . toString ( edge) ) ; } } public int getWeight ( int v1, int v2) { return edges[ v1] [ v2] ; } public void insertVertex ( String vertex) { vertexList. add ( vertex) ; } public void insertEdge ( int v1, int v2, int weight) { if ( v1 >= edges. length || v2 >= edges. length) { throw new RuntimeException ( "该结点标号越界" ) ; } edges[ v1] [ v2] = weight; edges[ v2] [ v1] = weight; numOfEdges++ ; } }
四、图的遍历
4.1 图的深度优先遍历(DFS)
4.2 图的广度优先遍历(BFS)
package com. gyh. grapg ; import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. LinkedList ;
import java. util. Queue ;
public class Graph { private ArrayList < String > vertexList; private int [ ] [ ] edges; private int numOfEdges = 0 ; private boolean [ ] isVisited; public static void main ( String [ ] args) { int n = 5 ; String [ ] VertexValue = { "A" , "B" , "C" , "D" , "E" } ; Graph graph = new Graph ( n) ; for ( String s : VertexValue ) { graph. insertVertex ( s) ; } graph. insertEdge ( 0 , 1 , 1 ) ; graph. insertEdge ( 0 , 2 , 1 ) ; graph. insertEdge ( 1 , 2 , 1 ) ; graph. insertEdge ( 1 , 3 , 1 ) ; graph. insertEdge ( 1 , 4 , 1 ) ; graph. showGraph ( ) ;
graph. bfs ( 2 , new LinkedList < > ( ) ) ; } public void bfs ( int v, Queue < Integer > queue) { queue. add ( v) ; isVisited[ v] = true ; while ( queue. size ( ) > 0 ) { v = queue. poll ( ) ; System . out. println ( vertexList. get ( v) ) ; for ( int i = 0 ; i < edges. length; i++ ) { if ( edges[ v] [ i] != 0 && ! isVisited[ i] ) { queue. add ( i) ; isVisited[ i] = true ; } } } } public void dfs ( int v) { if ( v < 0 || v >= edges. length) { return ; } System . out. println ( vertexList. get ( v) ) ; isVisited[ v] = true ; for ( int i = 0 ; i < edges. length; i++ ) { if ( edges[ v] [ i] != 0 && ! isVisited[ i] ) { dfs ( i) ; } } } public Graph ( int n) { this . edges = new int [ n] [ n] ; vertexList = new ArrayList < > ( ) ; isVisited = new boolean [ n] ; } public int getNumOfVertex ( ) { return vertexList. size ( ) ; } public int getNumOfEdges ( ) { return numOfEdges; } public void showGraph ( ) { for ( int [ ] edge : edges) { System . out. println ( Arrays . toString ( edge) ) ; } } public int getWeight ( int v1, int v2) { return edges[ v1] [ v2] ; } public void insertVertex ( String vertex) { vertexList. add ( vertex) ; } public void insertEdge ( int v1, int v2, int weight) { if ( v1 >= edges. length || v2 >= edges. length) { throw new RuntimeException ( "该结点标号越界" ) ; } edges[ v1] [ v2] = weight; edges[ v2] [ v1] = weight; numOfEdges++ ; } }
4.3 图的深度优先 VS 广度优先