有向圖 在有向圖中,結點對<x ,y>是有序的,結點對<x,y>稱為從結點x到結點y的一條有向邊,因此,<x,y>與<y,x>是兩條不同的邊。有向圖中的結點對<x,y>用一對尖括號括起來,x是有向邊的始點,y是有向邊的終點,有向圖中的邊也稱作弧。
無向圖 在無向圖中,結點對(x,y)是無序的,結點對(x,y)稱為與結點x和結點y相關聯的一條邊。(x,y)等價於<x,y>和<y,x>。
完全圖 在有n個結點的無向圖中,若有n(n-1)/2條邊,即任意兩個結點之間有且只有一條邊,則稱此圖為無向完全圖。在有n個結點的有向圖中,若有n(n-1)條邊,即任意兩個結點之間有且只有方向相反的兩條邊,則稱此圖為有向完全圖。
鄰接結點 在無向圖G中,若(u,v)是E(G)中的一條邊,則稱u和v互為鄰接結點,並稱邊(u,v)依附於結點u和v。在有向圖G中,若<u,v>是E(G)中的一條邊,則稱結點u鄰接到結點v,結點v鄰接自結點u,並稱邊<u,v>和結點u和結點v相關聯。
結點的度 結點v的度是與它相關聯的邊的條數,記作TD(v)。
路徑 在圖G=(V,E)中,若從結點vi出發有一組邊使可到達結點vj,則稱結點vi到結點vj的結點序列為從結點vi到結點vj的路徑。
權 有些圖的邊附帶有數據信息,這些附帶的數據信息稱為權。第i條邊的權用符號wi表示。
路徑長度 對於不帶權的圖,一條路徑的路徑長度是指該路徑上的邊的條數;對於帶權的圖,一條路徑的路徑長度是指該路徑上各個邊權值的總和。
最小生成樹 一個有 n 個結點的連通圖的生成樹是原圖的極小連通子圖,且包含原圖中的所有 n 個結點,並且有保持圖聯通的最少的邊。(n-1)條邊。
圖的鄰接矩陣存儲結構
假設圖G=(V,E)有n個結點,即V={v0,v1,…,vn-1},E可用如下形式的矩陣A描述,對於A中的每一個元素aij,滿足:aij=1表示i和j節點有邊相連,aij=0表示i和j沒有邊相連。
由於矩陣A中的元素aij表示了結點vi和結點vj之間邊的關系,或者說,A中的元素aij表示了結點vi和結點vj(0≤j≤n-1)的鄰接關系,所以矩陣A稱作鄰接矩陣。 aij=多少的數表示i和j的路徑權值。
import java.util.ArrayList; //鄰接矩陣類 public class MyAdjGraphic { static final int maxWeight=-1; //如果兩個結點之間沒有邊,權值為-1; ArrayList vertices = new ArrayList();//存放結點的集合 int[][] edges; //鄰接矩陣的二維數組 int numOfEdges; //邊的數量 public MyAdjGraphic(int n) { edges = new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j) //對角線上的元素為0 { edges[i][j]=0; } else { edges[i][j]=maxWeight; } } } numOfEdges = 0; } //返回邊的數量 public int getNumOfEdges() { return this.numOfEdges; } //返回結點的數量 public int getNumOfVertice() { return this.vertices.size(); } //返回結點的值 public Object getValueOfVertice(int index) { return this.vertices.get(index); } //獲得某條邊的權值 public int getWeightOfEdges(int v1,int v2) throws Exception { if((v1 < 0 || v1 >= vertices.size())||(v2 < 0||v2 >= vertices.size())) { throw new Exception("v1或者v2參數越界錯誤!"); } return this.edges[v1][v2]; } //插入結點 public void insertVertice(Object obj) { this.vertices.add(obj); } //插入帶權值的邊 public void insertEdges(int v1,int v2,int weight) throws Exception { if((v1 < 0 || v1 >= vertices.size())||(v2 < 0||v2 >= vertices.size())) { throw new Exception("v1或者v2參數越界錯誤!"); } this.edges[v1][v2]=weight; this.numOfEdges++; } //刪除某條邊 public void deleteEdges(int v1,int v2) throws Exception { if((v1 < 0 || v1 >= vertices.size())||(v2 < 0||v2 >= vertices.size())) { throw new Exception("v1或者v2參數越界錯誤!"); } if( v1==v2 || this.edges[v1][v2]==maxWeight)//自己到自己的邊或者邊不存在則不用刪除。 { throw new Exception("邊不存在!"); } this.edges[v1][v2]=maxWeight; this.numOfEdges--; } //打印鄰接矩陣 public void print() { for(int i=0;i<this.edges.length;i++ ) { for(int j=0;j<this.edges[i].length;j++) { System.out.print(edges[i][j]+" "); } System.out.println(); } } } //插入的邊的類 public class Weight { int row; //起點 int col; //終點 int weight; //權值 Weight(int row,int col,int weight) { this.row = row; this.col = col; this.weight = weight; } public static void createAdjGraphic(MyAdjGraphic g, Object[] vertices, int n,Weight[] weight,int e) throws Exception { //初始化結點 for(int i=0;i<n;i++) { g.insertVertice(vertices[i]); } //初始化所有的邊 for(int i=0;i<e;i++) { g.insertEdges(weight[i].row, weight[i].col, weight[i].weight); } } } public class Test { public static void main(String[] args) { int n=5; //5個結點 int e=5; //5條邊 MyAdjGraphic g = new MyAdjGraphic(n); Object[] vertices = new Object[]{new Character('A'),new Character('B'),new Character('C'),new Character('D'),new Character('E')}; Weight[] weights = new Weight[]{new Weight(0,1,10),new Weight(0,4,20),new Weight(2,1,40),new Weight(1,3,30),new Weight(3,2,50)}; try { Weight.createAdjGraphic(g, vertices, n, weights, e); System.out.println("--------該臨街矩陣如下---------"); g.print(); System.out.println("結點的個數:"+g.getNumOfVertice()); System.out.println("邊的個數:"+g.getNumOfEdges()); g.deleteEdges(0, 4); System.out.println("--------刪除之后---------"); g.print(); System.out.println("結點的個數:"+g.getNumOfVertice()); System.out.println("邊的個數:"+g.getNumOfEdges()); } catch(Exception ex) { } } } /*--------該臨街矩陣如下--------- 0 10 -1 -1 20 -1 0 -1 30 -1 -1 40 0 -1 -1 -1 -1 50 0 -1 -1 -1 -1 -1 0 結點的個數:5 邊的個數:5 --------刪除之后--------- 0 10 -1 -1 -1 -1 0 -1 30 -1 -1 40 0 -1 -1 -1 -1 50 0 -1 -1 -1 -1 -1 0 結點的個數:5 邊的個數:4*/