Explorar el Código

add graph implementation.

171870002 hace 5 años
padre
commit
98afb5f3e5

+ 150 - 1
src/main/java/com/example/data_structure/service/GraphService.java

@@ -1,9 +1,158 @@
 package com.example.data_structure.service;
 
 
-import org.springframework.stereotype.Component;
+import com.example.data_structure.domain.UDUWGraph;
+import com.example.data_structure.domain.UDWGraph;
+import com.example.data_structure.vo.GraphTraversalStep;
+import com.example.data_structure.vo.GraphTraversalVO;
+import com.example.data_structure.vo.PrimMinimumSpanningTree;
 import org.springframework.stereotype.Service;
 
+import java.util.*;
+
 @Service
 public class GraphService {
+    private static final int NODE_NUM=7;
+    //获取一个新的无向非带权图
+    public UDUWGraph getNewUDUDGraph(){
+        ArrayList<ArrayList<Integer>> content=new ArrayList<ArrayList<Integer>>();
+        for(int i=0;i<NODE_NUM;i++){
+            ArrayList<Integer> raw=new ArrayList<Integer>();
+            for(int j=0;j<NODE_NUM;j++){
+                raw.add(0);
+            }
+            content.add(raw);
+        }
+        //11-21 0.7
+        int tokeTimes = Math.round(((float)Math.random())*10)+11;
+        int edgeNum=0;
+        for(int i=1;i<=tokeTimes;i++){
+            int left=Math.round(((float)Math.random())*7);
+            int right=Math.round(((float)Math.random())*7);
+            while(left==right){
+                right=Math.round(((float)Math.random())*7);
+            }
+            if(content.get(left).get(right)==1){
+                i--;
+            }else if(content.get(left).get(right)==0){
+                content.get(left).set(right,1);
+                content.get(right).set(left,1);
+                edgeNum++;
+            }
+
+        }
+        return new UDUWGraph(content);
+    }
+
+    //获取一个新的无向带权图
+    public UDWGraph getNewUDWGraph(){
+        ArrayList<ArrayList<Integer>> content=new ArrayList<ArrayList<Integer>>();
+        for(int i=0;i<NODE_NUM;i++){
+            ArrayList<Integer> raw=new ArrayList<Integer>();
+            for(int j=0;j<NODE_NUM;j++){
+                raw.add(0);
+            }
+            content.add(raw);
+        }
+        //11-21 0.7
+        int tokeTimes = Math.round(((float)Math.random())*10)+11;
+        int edgeNum=0;
+        for(int i=1;i<=tokeTimes;i++){
+            int left=Math.round(((float)Math.random())*7);
+            int right=Math.round(((float)Math.random())*7);
+            while(left==right){
+                right=Math.round(((float)Math.random())*7);
+            }
+            if(content.get(left).get(right)==1){
+                i--;
+            }else if(content.get(left).get(right)==0){
+                int weight=Math.round(((float)Math.random())*10);
+                content.get(left).set(right,weight);
+                content.get(right).set(left,weight);
+                edgeNum++;
+            }
+
+        }
+        return new UDWGraph(content);
+    }
+
+    //获取无向非带权图的广度优先遍历
+    public GraphTraversalVO getBreadthFirstTraversal(UDUWGraph graph,int startVertex){
+        ArrayList<ArrayList<Integer>> content=graph.getContent();
+        ArrayList<Integer> visitSequence =new ArrayList<Integer>();
+        ArrayList<Integer> visited=new ArrayList<Integer>();
+        ArrayList<Integer> visitStartNode=new ArrayList<Integer>();
+        for(int i=0;i<content.size();i++){
+            System.out.println("=================add===============");
+            visitStartNode.add(-1);
+        }
+
+        //访问某节点的起初节点
+        visitStartNode.set(startVertex,startVertex);
+
+        GraphTraversalVO graphTraversalVO=new GraphTraversalVO();
+        graphTraversalVO.setGraph(graph);
+        ArrayList<GraphTraversalStep> steps=new ArrayList<GraphTraversalStep>();
+        for(int i=0;i<graph.getNodeNum();i++){
+            visited.add(0);
+        }
+        Queue<Integer> q=new LinkedList<Integer>();
+        q.add(startVertex);
+        while(!q.isEmpty()){
+            int curVertex=q.poll();
+            steps.add(new GraphTraversalStep("visit",visitStartNode.get(curVertex),curVertex,true));
+            visited.set(curVertex,1);
+            visitSequence.add(curVertex);
+            for(int i=0;i<graph.getNodeNum();i++){
+                if(content.get(curVertex).get(i)==1){
+                    //如果节点没有被访问,且没有被纳入队列当中,则tryRoad为true
+                    if(visited.get(i)==0&&!q.contains(i)) {
+                        steps.add(new GraphTraversalStep("tryRoad",curVertex,i,true));
+                        if(visitStartNode.get(i)==-1){
+                            visitStartNode.set(i,curVertex);
+                        }
+                        q.offer(i);
+                    }else{
+                        steps.add(new GraphTraversalStep("tryRoad",curVertex,i,false));
+                    }
+                }
+            }
+        }
+        graphTraversalVO.setContent(steps);
+        return graphTraversalVO;
+    }
+
+    //获取无向非带权图的深度优先遍历
+    public GraphTraversalVO getDepthFirstTraversal(UDUWGraph graph,int startVertex){
+        int nodeNum=graph.getNodeNum();
+        ArrayList<ArrayList<Integer>> content=new ArrayList<ArrayList<Integer>>();
+        GraphTraversalVO graphTraversalVO =new GraphTraversalVO();
+        graphTraversalVO.setGraph(graph);
+        ArrayList<GraphTraversalStep> steps=new ArrayList<GraphTraversalStep>();
+        GraphTraversalStep currentStep=new GraphTraversalStep();
+
+        ArrayList<Integer> visitSequence=new ArrayList<Integer>();
+        ArrayList<Integer> visited=new ArrayList<Integer>();
+        ArrayList<Integer> visitStartNode=new ArrayList<Integer>();
+
+        for(int i=0;i<nodeNum;i++){
+            visited.add(0);
+        }
+        for(int i=0;i<nodeNum;i++){
+            visitStartNode.add(-1);
+        }
+        visitStartNode.set(startVertex,startVertex);
+        Stack<Integer> stack=new Stack<Integer>();
+        stack.push(startVertex);
+        while(!stack.isEmpty()){
+            boolean isTerminal=false;
+        }
+        return null;
+    }
+    //获取无向带权图的prim算法的最小生成树
+    public PrimMinimumSpanningTree getPrimMinimumSpanningTree(UDUWGraph graph,int startVertex){
+        return null;
+    }
+
+
 }

+ 0 - 51
src/main/java/com/example/data_structure/vo/GraphSearchStep.java

@@ -1,51 +0,0 @@
-package com.example.data_structure.vo;
-
-public class GraphSearchStep {
-    private String type; //有两个值,一个是visit,一个是tryRoad
-    private int Start;
-    private int end;
-
-    public GraphSearchStep(String type, int start, int end) {
-        this.type = type;
-        Start = start;
-        this.end = end;
-    }
-
-    public GraphSearchStep() {
-    }
-
-    //getter
-    public String getType() {
-        return type;
-    }
-
-    public int getStart() {
-        return Start;
-    }
-
-    public int getEnd() {
-        return end;
-    }
-
-    //setter
-    public void setType(String type) {
-        this.type = type;
-    }
-
-    public void setStart(int start) {
-        Start = start;
-    }
-
-    public void setEnd(int end) {
-        this.end = end;
-    }
-
-    @Override
-    public String toString() {
-        return "GraphSearchStep{" +
-                "type='" + type + '\'' +
-                ", Start=" + Start +
-                ", end=" + end +
-                '}';
-    }
-}

+ 74 - 0
src/main/java/com/example/data_structure/vo/GraphTraversalStep.java

@@ -0,0 +1,74 @@
+package com.example.data_structure.vo;
+
+public class GraphTraversalStep {
+    private String type; //广度优先遍历有两个值,一个是visit,一个是tryRoad;深度优先遍历多了一个back(后退一步)
+    private int Start;
+    private int end;
+    private boolean isSuccess; //若tryRoad的时候走的通(end节点没有被访问,也没在队列当中,说明end节点要通过这个start节点被访问)
+
+    public GraphTraversalStep(String type, int start, int end,boolean isSuccess) {
+        this.type = type;
+        Start = start;
+        this.end = end;
+        this.isSuccess=isSuccess;
+    }
+
+    public GraphTraversalStep() {
+    }
+
+    //getter
+    public String getType() {
+        return type;
+    }
+
+    public int getStart() {
+        return Start;
+    }
+
+    public int getEnd() {
+        return end;
+    }
+
+    public boolean isSuccess() {
+        return isSuccess;
+    }
+
+    //setter
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public void setStart(int start) {
+        Start = start;
+    }
+
+    public void setEnd(int end) {
+        this.end = end;
+    }
+
+    public void setSuccess(boolean success) {
+        this.isSuccess = success;
+    }
+
+    @Override
+    public String toString() {
+        return "GraphTraversalStep{" +
+                "type='" + type + '\'' +
+                ", Start=" + Start +
+                ", end=" + end +
+                ", isSuccess=" + isSuccess +
+                '}';
+    }
+
+    //copy step
+    public GraphTraversalStep copyGraphTraversalStep(GraphTraversalStep step){
+        GraphTraversalStep graphTraversalStep=new GraphTraversalStep();
+        graphTraversalStep.setType(step.getType());
+        graphTraversalStep.setStart(step.getStart());
+        graphTraversalStep.setEnd(step.getEnd());
+        graphTraversalStep.setSuccess(step.isSuccess);
+        return graphTraversalStep;
+    }
+
+
+}

+ 7 - 7
src/main/java/com/example/data_structure/vo/GraphSearchVO.java → src/main/java/com/example/data_structure/vo/GraphTraversalVO.java

@@ -4,16 +4,16 @@ import com.example.data_structure.domain.UDUWGraph;
 
 import java.util.ArrayList;
 
-public class GraphSearchVO {
+public class GraphTraversalVO {
     private UDUWGraph graph;
-    private ArrayList<GraphSearchStep> content;
+    private ArrayList<GraphTraversalStep> content;
 
-    public GraphSearchVO(UDUWGraph graph, ArrayList<GraphSearchStep> content) {
+    public GraphTraversalVO(UDUWGraph graph, ArrayList<GraphTraversalStep> content) {
         this.graph = graph;
         this.content = content;
     }
 
-    public GraphSearchVO() {
+    public GraphTraversalVO() {
     }
 
     //getter
@@ -21,7 +21,7 @@ public class GraphSearchVO {
         return graph;
     }
 
-    public ArrayList<GraphSearchStep> getContent() {
+    public ArrayList<GraphTraversalStep> getContent() {
         return content;
     }
 
@@ -30,13 +30,13 @@ public class GraphSearchVO {
         this.graph = graph;
     }
 
-    public void setContent(ArrayList<GraphSearchStep> content) {
+    public void setContent(ArrayList<GraphTraversalStep> content) {
         this.content = content;
     }
 
     @Override
     public String toString() {
-        return "GraphSearchVO{" +
+        return "GraphTraversalVO{" +
                 "graph=" + graph +
                 ", content=" + content +
                 '}';

+ 4 - 0
src/main/java/com/example/data_structure/vo/PrimMinimumSpanningTree.java

@@ -0,0 +1,4 @@
+package com.example.data_structure.vo;
+
+public class PrimMinimumSpanningTree {
+}

+ 28 - 0
src/test/java/com/example/data_structure/service/GraphServiceTest.java

@@ -1,11 +1,39 @@
 package com.example.data_structure.service;
 
+import com.example.data_structure.domain.UDUWGraph;
+import com.example.data_structure.domain.UDWGraph;
+import com.example.data_structure.vo.GraphTraversalVO;
+import com.example.data_structure.vo.PrimMinimumSpanningTree;
+import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.util.ArrayList;
+
 @SpringBootTest
 public class GraphServiceTest {
 
     @Autowired
     GraphService graphService;
+
+    UDUWGraph makeAGraph(String[] rows){
+        ArrayList<ArrayList<Integer>> content=new ArrayList<ArrayList<Integer>>();
+        for(int i=0;i<rows.length;i++){
+            ArrayList<Integer> arr=new ArrayList<Integer>();
+            String[] row=rows[i].split(" ");
+            for(int j=0;j<row.length;j++){
+                arr.add(Integer.valueOf(row[j]));
+            }
+            content.add(arr);
+        }
+        return new UDUWGraph(content);
+    }
+
+    @Test
+    void breadthFirstTraversalTest(){
+        String[] matrix={"0 0 0 1 1 0 0","0 0 0 0 0 0 1","0 0 0 0 1 1 0","1 0 0 0 0 1 0","1 0 1 0 0 0 0","0 0 1 1 0 0 0","0 1 0 0 0 0 0"};
+        UDUWGraph graph=makeAGraph(matrix);
+        GraphTraversalVO vo=graphService.getBreadthFirstTraversal(graph,0);
+        System.out.println(vo.toString());
+    }
 }