public GraphDS(int nV, int nE) { this.V = nV; this.E = nE; Edges = new EdgeDS[E]; for (int i = 0; i < E; i++) { Edges[i] = new EdgeDS(); } }
public EdgeDS[] KruskalMST() { // This will store the resultant MST EdgeDS[] mstEdges = new EdgeDS[myGraph.V - 1]; for (int i = 0; i < myGraph.V - 1; i++) { mstEdges[0] = new EdgeDS(); } // Step 1: Sort all the edges in non-decreasing order of their weight. // If we are not allowed to change the given graph, we can create a copy of array of edges. Array.Sort(myGraph.Edges); // Step 2: Allocate memory for creating V ssubsets SubsetDS[] subsets = new SubsetDS[myGraph.V]; for (int i = 0; i < myGraph.V; i++) { subsets[i] = new SubsetDS(i, 0); } // Step 3: Number of Edges to be taken is V - 1 int edgesCounter = 0; int nERequired = 0; while (nERequired < myGraph.V - 1) { // Step 4: Pick the smallest edge. And increment the index for next iteration EdgeDS edge = new EdgeDS(); edge = myGraph.Edges[edgesCounter++]; int xRoot = myGraph.FindRoot(subsets, edge.Src); int yRoot = myGraph.FindRoot(subsets, edge.Dest); // Step 5: If including this edge does't cause cycle, include it in result and increment the index of result for next edge if (xRoot != yRoot) { mstEdges[nERequired++] = edge; myGraph.DoUnion(subsets, xRoot, yRoot); } } return(mstEdges); }
void TestLoadGraph() { int kern = cs.FindKernel("HookesLaw"); print(kern); cs.SetFloat("springLength", _springLength); cs.SetFloat("springk", springk); //RenderTexture tex = new RenderTexture(256, 256, 24); // renderTarget.enableRandomWrite = true; renderTarget.Create(); // // cs.SetTexture(kern, "res", renderTarget); // cs.Dispatch(kern, 50, 50, 1); // var nodeLookup = new Dictionary <string, Node>(); var sr = new System.IO.StringReader(_dataFile.text); //var lines = _dataFile.text.Split(new string[]{"\r\n", "\r", "\n"}, StringSplitOptions.None); while (true) { var line = sr.ReadLine(); if (line == null) { break; } if (line.Trim().Length > 0) { var tokens = line.Split(new string[] { "\t", " " }, StringSplitOptions.None); var n1tok = tokens[0]; var n2tok = tokens[1]; if (!nodeLookup.ContainsKey(n1tok)) { nodeLookup[n1tok] = CreateRandomNode(); } if (!nodeLookup.ContainsKey(n2tok)) { nodeLookup[n2tok] = CreateRandomNode(); } CreateEdge(nodeLookup[n1tok], nodeLookup[n2tok]); } if (_nodes.Count >= _maxGraphSize) { break; } } print("nodes: " + _nodes.Count); int nodeCount = _nodes.Count; adjacencyMatrix = new int[_nodes.Count * _nodes.Count]; nodeAcc = new Vector3[_nodes.Count * _nodes.Count]; nodeVel = new Vector3[_nodes.Count]; nodeAccBuffer = new ComputeBuffer(nodeAcc.Length, 3 * 4); nodeVelBuffer = new ComputeBuffer(nodeVel.Length, 3 * 4); for (int i = 0; i < nodeAcc.Length; ++i) { nodeAcc [i] = new Vector3(); adjacencyMatrix [i] = new int(); } for (int i = 0; i < nodeVel.Length; ++i) { nodeVel[i] = new Vector3(); } shaderInput = new NodeDS[_nodes.Count]; for (int i = 0; i < _nodes.Count; ++i) { shaderInput[i] = new NodeDS { pos = _nodes[i].pos, acc = new Vector3(), vel = _nodes[i].vel }; } dataBuffer = new ComputeBuffer(shaderInput.Length, 3 * 4 * 3); dataBuffer.SetData(shaderInput); shaderEdges = new EdgeDS[_edges.Count]; for (int i = 0; i < _edges.Count; ++i) { shaderEdges [i] = new EdgeDS(); shaderEdges [i].id1 = _nodes.IndexOf(_edges [i].Body1); shaderEdges [i].id2 = _nodes.IndexOf(_edges [i].Body2); adjacencyMatrix [nodeCount * shaderEdges [i].id2 + shaderEdges [i].id1] = 1; } edgeBuffer = new ComputeBuffer(shaderEdges.Length, 4 * 2); edgeBuffer.SetData(shaderEdges); adjacencyMatrixBuffer = new ComputeBuffer(adjacencyMatrix.Length, 4); adjacencyMatrixBuffer.SetData(adjacencyMatrix); for (int i = 0; i < nodeCount; ++i) { shaderInput[i].pos = _nodes[i].pos; shaderInput[i].acc = new Vector3(); shaderInput[i].vel = _nodes[i].vel; } }