// void setVertexRule(int i, int j) // { // if(i == 0) // { // vertices[i,j].setEdgeRule('N',false); // } // if(i == size-1) // { // vertices[i,j].setEdgeRule('S',false); // } // if(j == 0) // { // vertices[i,j].setEdgeRule('W',false); // } // if(j == size-1) // { // vertices[i,j].setEdgeRule('E',false); // } // } void initEdges() { edgeScript e = edgeGO.GetComponent <edgeScript>(); for (int i = 0; i < numOfEdges; i++) { edges[i] = e; //edges[i].setActive(false); } }
void createEdge(int graphNo, int i1, int j1, int i2, int j2, GameObject e, edgeScript es, vertexScript vsA, vertexScript vsB) { e.name = graphNo + " - [" + i1 + "," + j1 + "]-[" + i2 + "," + j2 + "]"; //e.SetActive(false); e.GetComponent <Renderer>().enabled = false; es.setWeight(UnityEngine.Random.Range(1, 101)); es.setA(vsA.getName()); es.setB(vsB.getName()); es.setGraphNo(graphNo); //edges[edgesInArray] = es; edges.Add(es); edgesInArray++; vsA.addEdge(es); }
public int isEdgeConnected(edgeScript e) { if (e.vertexBRow == e.vertexARow - 1 && e.vertexBCol == e.vertexACol - 1) { return(0); } else if (e.vertexBRow == e.vertexARow - 1 && e.vertexBCol == e.vertexACol) { return(1); } else if (e.vertexBRow == e.vertexARow - 1 && e.vertexBCol == e.vertexACol + 1) { return(2); } else if (e.vertexBRow == e.vertexARow && e.vertexBCol == e.vertexACol - 1) { return(3); } else if (e.vertexBRow == e.vertexARow && e.vertexBCol == e.vertexACol + 1) { return(4); } else if (e.vertexBRow == e.vertexARow + 1 && e.vertexBCol == e.vertexACol - 1) { return(5); } else if (e.vertexBRow == e.vertexARow + 1 && e.vertexBCol == e.vertexACol) { return(6); } else if (e.vertexBRow == e.vertexARow + 1 && e.vertexBCol == e.vertexACol + 1) { return(7); } else { return(-1); } }
public bool isTheSame(edgeScript e) { bool exists = false; if (getAR() == e.getAR()) { if (getAC() == e.getAC()) { if (getBR() == e.getBR()) { if (getBC() == e.getBC()) { exists = true; } } } } return(exists); }
List <edgeScript> kruskalMST(List <edgeScript> edges) { List <edgeScript> result = new List <edgeScript>(); int e = 0; int i = 0; List <edgeScript> sortedEdges = edges.OrderBy(se => se.getWeight()).ToList(); subset[] subsets = new subset[size * size]; for (i = 0; i < (size * size); i++) { subsets[i] = new subset(); } for (int v = 0; v < (size * size); v++) { subsets[v].parent = v; subsets[v].rank = 0; } i = 0; while (e < (size * size) - 1) { edgeScript next_edge = sortedEdges[i++]; int x = find(subsets, next_edge.getA()); int y = find(subsets, next_edge.getB()); if (x != y) { result.Add(next_edge); e++; union(subsets, x, y); } } return(result); }
bool edgeExists(edgeScript e) { bool exists = false; for (int i = 0; i < edges.Count; i++) { if (edges[i] != null) { if (e.isTheSame(edges[i])) { exists = true; } else if (e.isMirroredEdge(edges[i])) { exists = true; } } } return(exists); }
List <edgeScript> initEdgesFromFile() { List <edgeScript> edges = new List <edgeScript>(); string path; if (graphNo == 1) { path = "Assets/vertices1.json"; } else { path = "Assets/vertices2.json"; } string[] vertexData; int[][] vertexDataInt; StreamReader reader = new StreamReader(path); string verticesFromFile = reader.ReadToEnd(); reader.Close(); verticesFromFile = verticesFromFile.Substring(1, verticesFromFile.Length - 2); vertexData = verticesFromFile.Split(']'); vertexDataInt = new int[vertexData.Length - 1][]; for (int i = 0; i < vertexData.Length; i++) { vertexData[i] = vertexData[i].Trim(' ', ',', '['); if (i < vertexData.Length - 1) { vertexDataInt[i] = stringToInt(vertexData[i]); } } int counter = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { string name = graphNo + " - " + i + "," + j; GameObject v = GameObject.Find(name); vertexScript vs = v.GetComponent <vertexScript>(); if (i > 0 && horAndVer)//Add North Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3(j, (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90)); } else { edge = Instantiate(edgeGO, new Vector3((j + size), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i - 1), j); if (vertexDataInt[counter][1] == 1) { string nameB = graphNo + " - " + (i - 1) + "," + j; GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i - 1), j, edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (i > 0 && j > 0 && diag)//Add North-West Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i - 1), (j - 1)); if (vertexDataInt[counter][0] == 1) { string nameB = graphNo + " - " + (i - 1) + "," + (j - 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i - 1), (j - 1), edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (i > 0 && j < size - 1 && diag)//Add North-East Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i - 1), (j + 1)); if (vertexDataInt[counter][2] == 1) { string nameB = graphNo + " - " + (i - 1) + "," + (j + 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i - 1), (j + 1), edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (i < size - 1 && horAndVer)//Add South Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3(j, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90)); } else { edge = Instantiate(edgeGO, new Vector3(j + size, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i + 1), j); if (vertexDataInt[counter][6] == 1) { string nameB = graphNo + " - " + (i + 1) + "," + j; GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i + 1), j, edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (i < size - 1 && j > 0 && diag)//Add South-West Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i + 1), (j - 1)); if (vertexDataInt[counter][5] == 1) { string nameB = graphNo + " - " + (i + 1) + "," + (j - 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i + 1), (j - 1), edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (i < size - 1 && j < size - 1 && diag)//Add South-East Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i + 1), (j + 1)); if (vertexDataInt[counter][7] == 1) { string nameB = graphNo + " - " + (i + 1) + "," + (j + 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i + 1), (j + 1), edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (j > 0 && horAndVer)//Add West Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j - 0.5f), -i, 0), Quaternion.identity); } else { edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), -i, 0), Quaternion.identity); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, i, (j - 1)); if (vertexDataInt[counter][3] == 1) { string nameB = graphNo + " - " + i + "," + (j - 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, i, (j - 1), edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } if (j < size - 1 && horAndVer)//Add East Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j + 0.5f), -i, 0), Quaternion.identity); } else { edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), -i, 0), Quaternion.identity); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, i, (j + 1)); if (vertexDataInt[counter][4] == 1) { string nameB = graphNo + " - " + i + "," + (j + 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, i, (j + 1), edge, e, vs, vsB); edges.Add(e); } else { Destroy(edge); } } counter++; } } return(edges); }
void initEdges() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { string name = graphNo + " - " + i + "," + j; GameObject v = GameObject.Find(name); vertexScript vs = v.GetComponent <vertexScript>(); if (i > 0 && horAndVer)//Add North Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3(j, (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90)); } else { edge = Instantiate(edgeGO, new Vector3((j + size), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 90)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i - 1), j); if (!edgeExists(e)) { string nameB = graphNo + " - " + (i - 1) + "," + j; GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i - 1), j, edge, e, vs, vsB); } else { Destroy(edge); } } if (i > 0 && j > 0 && diag)//Add North-West Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, -45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i - 1), (j - 1)); if (!edgeExists(e)) { string nameB = graphNo + " - " + (i - 1) + "," + (j - 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i - 1), (j - 1), edge, e, vs, vsB); } else { Destroy(edge); } } if (i > 0 && j < size - 1 && diag)//Add North-East Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i + 0.5f), 0), Quaternion.Euler(0, 0, 45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i - 1), (j + 1)); if (!edgeExists(e)) { string nameB = graphNo + " - " + (i - 1) + "," + (j + 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i - 1), (j + 1), edge, e, vs, vsB); } else { Destroy(edge); } } if (i < size - 1 && horAndVer)//Add South Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3(j, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90)); } else { edge = Instantiate(edgeGO, new Vector3(j + size, (-i - 0.5f), 0), Quaternion.Euler(0, 0, 90)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i + 1), j); if (!edgeExists(e)) { string nameB = graphNo + " - " + (i + 1) + "," + j; GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i + 1), j, edge, e, vs, vsB); } else { Destroy(edge); } } if (i < size - 1 && j > 0 && diag)//Add South-West Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, 45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i + 1), (j - 1)); if (!edgeExists(e)) { string nameB = graphNo + " - " + (i + 1) + "," + (j - 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i + 1), (j - 1), edge, e, vs, vsB); } else { Destroy(edge); } } if (i < size - 1 && j < size - 1 && diag)//Add South-East Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45)); } else { edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), (-i - 0.5f), 0), Quaternion.Euler(0, 0, -45)); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, (i + 1), (j + 1)); if (!edgeExists(e)) { string nameB = graphNo + " - " + (i + 1) + "," + (j + 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, (i + 1), (j + 1), edge, e, vs, vsB); } else { Destroy(edge); } } if (j > 0 && horAndVer)//Add West Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j - 0.5f), -i, 0), Quaternion.identity); } else { edge = Instantiate(edgeGO, new Vector3((j + size - 0.5f), -i, 0), Quaternion.identity); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, i, (j - 1)); if (!edgeExists(e)) { string nameB = graphNo + " - " + i + "," + (j - 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, i, (j - 1), edge, e, vs, vsB); } else { Destroy(edge); } } if (j < size - 1 && horAndVer)//Add East Edge { GameObject edge; if (graphNo == 1) { edge = Instantiate(edgeGO, new Vector3((j + 0.5f), -i, 0), Quaternion.identity); } else { edge = Instantiate(edgeGO, new Vector3((j + size + 0.5f), -i, 0), Quaternion.identity); } edge.transform.parent = v.transform; edgeScript e = edge.GetComponent <edgeScript>(); e.setVertices(i, j, i, (j + 1)); if (!edgeExists(e)) { string nameB = graphNo + " - " + i + "," + (j + 1); GameObject vB = GameObject.Find(nameB); vertexScript vsB = vB.GetComponent <vertexScript>(); createEdge(graphNo, i, j, i, (j + 1), edge, e, vs, vsB); } else { Destroy(edge); } } //vertices[i,j] = vs; } } }
public void addEdge(edgeScript e) { edges.Add(e); }