
- Java實現的DSA教程
- Java實現的DSA - 首頁
- Java實現的DSA - 概述
- Java實現的DSA - 環境搭建
- Java實現的DSA - 演算法
- Java實現的DSA - 資料結構
- Java實現的DSA - 陣列
- Java實現的DSA - 連結串列
- Java實現的DSA - 雙向連結串列
- Java實現的DSA - 迴圈連結串列
- Java實現的DSA - 棧
- DSA - 表示式解析
- Java實現的DSA - 佇列
- Java實現的DSA - 優先佇列
- Java實現的DSA - 樹
- Java實現的DSA - 散列表
- Java實現的DSA - 堆
- Java實現的DSA - 圖
- Java實現的DSA - 搜尋技術
- Java實現的DSA - 排序技術
- Java實現的DSA - 遞迴
- Java實現的DSA有用資源
- Java實現的DSA - 快速指南
- Java實現的DSA - 有用資源
- Java實現的DSA - 討論
Java實現的DSA - 圖
概述
圖是一種用於模擬數學圖的資料結構。它由一組稱為頂點邊的連線對組成。我們可以使用頂點陣列和二維邊陣列來表示圖。
重要術語
頂點 − 圖的每個節點都表示為一個頂點。在下面的示例中,帶標籤的圓圈代表頂點。因此,A到G都是頂點。我們可以像下圖所示那樣使用陣列來表示它們。這裡A可以用索引0表示,B可以用索引1表示,以此類推。
邊 − 邊表示兩個頂點之間的路徑或兩個頂點之間的線。在下面的示例中,從A到B、B到C等的線表示邊。我們可以使用二維陣列來表示陣列,如下圖所示。這裡AB可以用第0行第1列的1來表示,BC可以用第1行第2列的1來表示,以此類推,其他組合保持為0。
鄰接 − 如果兩個節點或頂點透過一條邊連線在一起,則它們是鄰接的。在下面的示例中,B與A鄰接,C與B鄰接,以此類推。
路徑 − 路徑表示兩個頂點之間的一系列邊。在下面的示例中,ABCD表示從A到D的路徑。
基本操作
以下是圖的基本主要操作。
新增頂點 − 向圖中新增一個頂點。
新增邊 − 在圖的兩個頂點之間新增一條邊。
顯示頂點 − 顯示圖的一個頂點。
新增頂點操作
//add vertex to the array of vertex public void addVertex(char label){ lstVertices[vertexCount++] = new Vertex(label); }
新增邊操作
//add edge to edge array public void addEdge(int start,int end){ adjMatrix[start][end] = 1; adjMatrix[end][start] = 1; }
顯示邊操作
//display the vertex public void displayVertex(int vertexIndex){ System.out.print(lstVertices[vertexIndex].label+" "); }
遍歷演算法
以下是圖上的重要遍歷演算法。
深度優先搜尋 − 以深度優先的方式遍歷圖。
廣度優先搜尋 − 以廣度優先的方式遍歷圖。
深度優先搜尋演算法
深度優先搜尋演算法 (DFS) 以深度優先的方式遍歷圖,並使用棧來記住當任何迭代中出現死衚衕時下一個要開始搜尋的頂點。
如上例所示,DFS演算法首先從A遍歷到B,再到C,再到D,然後到E,然後到F,最後到G。它採用以下規則。
規則1 − 訪問相鄰的未訪問頂點。標記為已訪問。顯示它。將其壓入棧中。
規則2 − 如果未找到相鄰頂點,則從棧中彈出頂點。(它將彈出棧中所有沒有相鄰頂點的頂點。)
規則3 − 重複規則1和規則2,直到棧為空。
public void depthFirstSearch(){ //mark first node as visited lstVertices[0].visited = true; //display the vertex displayVertex(0); //push vertex index in stack stack.push(0); while(!stack.isEmpty()){ //get the unvisited vertex of vertex which is at top of the stack int unvisitedVertex = getAdjUnvisitedVertex(stack.peek()); //no adjacent vertex found if(unvisitedVertex == -1){ stack.pop(); }else{ lstVertices[unvisitedVertex].visited = true; displayVertex(unvisitedVertex); stack.push(unvisitedVertex); } } //stack is empty, search is complete, reset the visited flag for(int i=0;i<vertexCount;i++){ lstVertices[i].visited = false; } }
廣度優先搜尋演算法
廣度優先搜尋演算法 (BFS) 以廣度優先的方式遍歷圖,並使用佇列來記住當任何迭代中出現死衚衕時下一個要開始搜尋的頂點。
如上例所示,BFS演算法首先從A遍歷到B,再到E,再到F,然後到C和G,最後到D。它採用以下規則。
規則1 − 訪問相鄰的未訪問頂點。標記為已訪問。顯示它。將其插入佇列中。
規則2 − 如果未找到相鄰頂點,則從佇列中刪除第一個頂點。
規則3 − 重複規則1和規則2,直到佇列為空。
public void breadthFirstSearch(){ //mark first node as visited lstVertices[0].visited = true; //display the vertex displayVertex(0); //insert vertex index in queue queue.insert(0); int unvisitedVertex; while(!queue.isEmpty()){ //get the unvisited vertex of vertex which is at front of the queue int tempVertex = queue.remove(); //no adjacent vertex found while((unvisitedVertex=getAdjUnvisitedVertex(tempVertex)) != -1){ lstVertices[unvisitedVertex].visited = true; displayVertex(unvisitedVertex); queue.insert(unvisitedVertex); } } //queue is empty, search is complete, reset the visited flag for(int i=0;i<vertexCount;i++){ lstVertices[i].visited = false; } }
圖的實現
Stack.java
package com.tutorialspoint.datastructure; public class Stack { private int size; // size of the stack private int[] intArray; // stack storage private int top; // top of the stack // Constructor public Stack(int size){ this.size = size; intArray = new int[size]; //initialize array top = -1; //stack is initially empty } // Operation : Push // push item on the top of the stack public void push(int data) { if(!isFull()){ // increment top by 1 and insert data intArray[++top] = data; }else{ System.out.println("Cannot add data. Stack is full."); } } // Operation : Pop // pop item from the top of the stack public int pop() { //retrieve data and decrement the top by 1 return intArray[top--]; } // Operation : Peek // view the data at top of the stack public int peek() { //retrieve data from the top return intArray[top]; } // Operation : isFull // return true if stack is full public boolean isFull(){ return (top == size-1); } // Operation : isEmpty // return true if stack is empty public boolean isEmpty(){ return (top == -1); } }
Queue.java
package com.tutorialspoint.datastructure; public class Queue { private final int MAX; private int[] intArray; private int front; private int rear; private int itemCount; public Queue(int size){ MAX = size; intArray = new int[MAX]; front = 0; rear = -1; itemCount = 0; } public void insert(int data){ if(!isFull()){ if(rear == MAX-1){ rear = -1; } intArray[++rear] = data; itemCount++; } } public int remove(){ int data = intArray[front++]; if(front == MAX){ front = 0; } itemCount--; return data; } public int peek(){ return intArray[front]; } public boolean isEmpty(){ return itemCount == 0; } public boolean isFull(){ return itemCount == MAX; } public int size(){ return itemCount; } }
Vertex.java
package com.tutorialspoint.datastructure; public class Vertex { public char label; public boolean visited; public Vertex(char label){ this.label = label; visited = false; } }
Graph.java
package com.tutorialspoint.datastructure; public class Graph { private final int MAX = 20; //array of vertices private Vertex lstVertices[]; //adjacency matrix private int adjMatrix[][]; //vertex count private int vertexCount; private Stack stack; private Queue queue; public Graph(){ lstVertices = new Vertex[MAX]; adjMatrix = new int[MAX][MAX]; vertexCount = 0; stack = new Stack(MAX); queue = new Queue(MAX); for(int j=0; j<MAX; j++) // set adjacency for(int k=0; k<MAX; k++) // matrix to 0 adjMatrix[j][k] = 0; } //add vertex to the vertex list public void addVertex(char label){ lstVertices[vertexCount++] = new Vertex(label); } //add edge to edge array public void addEdge(int start,int end){ adjMatrix[start][end] = 1; adjMatrix[end][start] = 1; } //display the vertex public void displayVertex(int vertexIndex){ System.out.print(lstVertices[vertexIndex].label+" "); } //get the adjacent unvisited vertex public int getAdjUnvisitedVertex(int vertexIndex){ for(int i=0; i<vertexCount; i++) if(adjMatrix[vertexIndex][i]==1 && lstVertices[i].visited==false) return i; return -1; } public void depthFirstSearch(){ //mark first node as visited lstVertices[0].visited = true; //display the vertex displayVertex(0); //push vertex index in stack stack.push(0); while(!stack.isEmpty()){ //get the unvisited vertex of vertex which is at top of the stack int unvisitedVertex = getAdjUnvisitedVertex(stack.peek()); //no adjacent vertex found if(unvisitedVertex == -1){ stack.pop(); }else{ lstVertices[unvisitedVertex].visited = true; displayVertex(unvisitedVertex); stack.push(unvisitedVertex); } } //stack is empty, search is complete, reset the visited flag for(int i=0;i<vertexCount;i++){ lstVertices[i].visited = false; } } public void breadthFirstSearch(){ //mark first node as visited lstVertices[0].visited = true; //display the vertex displayVertex(0); //insert vertex index in queue queue.insert(0); int unvisitedVertex; while(!queue.isEmpty()){ //get the unvisited vertex of vertex which is at front of the queue int tempVertex = queue.remove(); //no adjacent vertex found while((unvisitedVertex=getAdjUnvisitedVertex(tempVertex)) != -1){ lstVertices[unvisitedVertex].visited = true; displayVertex(unvisitedVertex); queue.insert(unvisitedVertex); } } //queue is empty, search is complete, reset the visited flag for(int i=0;i<vertexCount;i++){ lstVertices[i].visited = false; } } }
演示程式
GraphDemo.java
package com.tutorialspoint.datastructure; public class GraphDemo { public static void main(String args[]){ Graph graph = new Graph(); graph.addVertex('A'); //0 graph.addVertex('B'); //1 graph.addVertex('C'); //2 graph.addVertex('D'); //3 graph.addVertex('E'); //4 graph.addVertex('F'); //5 graph.addVertex('G'); //6 /* 1 2 3 * 0 |--B--C--D * A--| * | * | 4 * |-----E * | 5 6 * | |--F--G * |--| */ graph.addEdge(0, 1); //AB graph.addEdge(1, 2); //BC graph.addEdge(2, 3); //CD graph.addEdge(0, 4); //AC graph.addEdge(0, 5); //AF graph.addEdge(5, 6); //FG System.out.print("Depth First Search: "); //A B C D E F G graph.depthFirstSearch(); System.out.println(""); System.out.print("Breadth First Search: "); //A B E F C G D graph.breadthFirstSearch(); } }
如果我們編譯並執行上述程式,則會產生以下結果:
Depth First Search: A B C D E F G Breadth First Search: A B E F C G D