1 |
|
2 | const {Queue , HashMap , dijkstra , PriorityQueue , isconnected
|
3 | , Graph , GraphVertex , HashSet , MinHeap} = require("./dist/data-structure");
|
4 |
|
5 | function topoSort(graph){
|
6 | if (!graph){
|
7 | return [];
|
8 | }
|
9 | const clonedGraph = graph;
|
10 | const vertices = clonedGraph.getVertexs();
|
11 | const queue = new PriorityQueue();
|
12 | vertices.forEach(item => queue.enqueue(item, -item.getInDegree()));
|
13 | const topoSortedArr = [];
|
14 | while (!queue.isEmpty()){
|
15 | const {Value: vertex , Priority: indegree} = queue.dequeue();
|
16 | if (indegree < 0){
|
17 | throw new Error("Cyclic dependency " + vertex.Key);
|
18 | }
|
19 | topoSortedArr.push(vertex);
|
20 | const head = vertex.getEdges();
|
21 | while (head.Size){
|
22 | const edge = head.getHeadNode().Value;
|
23 | vertex.deleteEdge(edge);
|
24 | queue.changePriority(edge.EndVertex, -edge.EndVertex.getInDegree());
|
25 | }
|
26 | }
|
27 | return topoSortedArr;
|
28 | }
|
29 |
|
30 | const vertexA = new GraphVertex("A");
|
31 | const vertexB = new GraphVertex("B");
|
32 | const vertexC = new GraphVertex("C");
|
33 | const vertexD = new GraphVertex("D");
|
34 | const vertexE = new GraphVertex("E");
|
35 | const vertexF = new GraphVertex("F");
|
36 | vertexA.addEdge(vertexB);
|
37 | vertexA.addEdge(vertexC);
|
38 | vertexA.addEdge(vertexD);
|
39 | vertexC.addEdge(vertexB);
|
40 | vertexC.addEdge(vertexE);
|
41 | vertexD.addEdge(vertexE);
|
42 | vertexF.addEdge(vertexD);
|
43 | vertexF.addEdge(vertexE);
|
44 | const graph = new Graph();
|
45 | graph
|
46 | .addVertex(vertexA)
|
47 | .addVertex(vertexB)
|
48 | .addVertex(vertexC)
|
49 | .addVertex(vertexD)
|
50 | .addVertex(vertexE)
|
51 | .addVertex(vertexF);
|
52 |
|
53 | const vertexs = topoSort(graph);
|
54 |
|
55 | // const edgeSet = new HashSet();
|
56 | // function getEulerCircuit(
|
57 | // graph,
|
58 | // edges,
|
59 | // startVertex,
|
60 | // existHashMap,
|
61 | // prevKey){
|
62 | // if (!startVertex){
|
63 | // startVertex = graph.getVertexs()[0];
|
64 | // }
|
65 | // if (!edges){
|
66 | // edges = [];
|
67 | // }
|
68 | // if (!startVertex || !graph.findVertex(startVertex.Key)){
|
69 | // return [];
|
70 | // }
|
71 | // existHashMap = existHashMap || new HashMap(graph.getKeys().length);
|
72 | // const nextNodes = startVertex.getNeighbors();
|
73 | // existHashMap.put(startVertex.Key , true);
|
74 | // nextNodes.forEach(item => {
|
75 | // if(item.Key === prevKey){
|
76 | // return;
|
77 | // }
|
78 | // const edgeKey = JSON.stringify([startVertex.Key,item.Key].sort((a,b) => a>b?1:-1));
|
79 | // if(!edgeSet.has(edgeKey)){
|
80 | // if (existHashMap.get(item.Key)){
|
81 | // edges.push(startVertex.getEdge(item.Key));
|
82 | // }else{
|
83 | // existHashMap.put(item.Key,true);
|
84 | // getEulerCircuit(graph , edges, item , existHashMap , startVertex.Key);
|
85 | // edges.push(startVertex.getEdge(item.Key));
|
86 | // }
|
87 | // edgeSet.add(edgeKey);
|
88 | // }
|
89 | // });
|
90 | // return edges;
|
91 | // }
|
92 | // const graph = new Graph(false);
|
93 | // const vertexs = Array.from({length:10},(item,index) => new GraphVertex(index.toString()));
|
94 | // vertexs.forEach(item => graph.addVertex(item));
|
95 | // graph
|
96 | // .addEdge(vertexs[0],vertexs[8])
|
97 | // .addEdge(vertexs[0],vertexs[9])
|
98 | // .addEdge(vertexs[8],vertexs[7])
|
99 | // .addEdge(vertexs[9],vertexs[2])
|
100 | // .addEdge(vertexs[9],vertexs[3])
|
101 | // .addEdge(vertexs[2],vertexs[1])
|
102 | // .addEdge(vertexs[3],vertexs[1])
|
103 | // .addEdge(vertexs[9],vertexs[7])
|
104 | // .addEdge(vertexs[7],vertexs[6])
|
105 | // .addEdge(vertexs[7],vertexs[5])
|
106 | // .addEdge(vertexs[6],vertexs[5]);
|
107 | // const edges = getEulerCircuit(graph);
|
108 | // console.log(edges);
|
109 | // const map = edges.map(item => [item.StartVertex.Key,item.EndVertex.Key]);
|
110 | // console.log(map);
|