public addEdge ( int p_id, string p_statement, int p_fromNode, int p_toNode ) : void | ||
p_id | int | |
p_statement | string | |
p_fromNode | int | |
p_toNode | int | |
return | void |
public static void AllPathInGraphMain() { string[] tokens_n = "4 4 3".Split(' '); int n = Convert.ToInt32(tokens_n[0]); int m = Convert.ToInt32(tokens_n[1]); int q = Convert.ToInt32(tokens_n[2]); Graph graph = new Graph(n); //for (int a0 = 0; a0 < m; a0++) { { graph.addEdge(1, 2); graph.addEdge(2, 3); graph.addEdge(2, 4); graph.addEdge(3, 1); } graph.CanMeet(1, 4, 2); // for (int a0 = 0; a0 < q; a0++) { // string[] tokens_u = Console.ReadLine().Split(' '); // int u = Convert.ToInt32(tokens_u[0]); // int v = Convert.ToInt32(tokens_u[1]); // int w = Convert.ToInt32(tokens_u[2]); // graph.GetAllPaths(u, w); //} }
static void GraphTest() { Graph <int> curGraph = new Graph <int>(); curGraph.addVertex("A", 0); curGraph.addVertex("B", 1); curGraph.addVertex("C", 2); curGraph.addVertex("D", 3); curGraph.addVertex("E", 4); curGraph.addVertex("F", 5); curGraph.addEdge("A", "B"); curGraph.addEdge("B", "C"); curGraph.addEdge("D", "C"); curGraph.addEdge("E", "B"); curGraph.addEdge("D", "G"); Console.WriteLine(curGraph.distanceBetween("A", "B")); Console.WriteLine(curGraph.distanceBetween("A", "C")); Console.WriteLine(curGraph.distanceBetween("A", "D")); Console.WriteLine(curGraph.distanceBetween("C", "B")); Console.WriteLine(curGraph.distanceBetween("D", "B")); Console.WriteLine(curGraph.distanceBetween("D", "E")); Console.WriteLine(curGraph.distanceBetween("D", "F")); Console.WriteLine(curGraph.distanceBetween("D", "G")); Console.WriteLine(); Console.WriteLine(); curGraph.print(); Console.WriteLine(); Console.WriteLine(); Graph <int> testGraph = new Graph <int>(); testGraph.addVertex("A", 0); testGraph.addVertex("B", 1); testGraph.addVertex("C", 2); testGraph.addEdge("A", "B"); testGraph.addEdge("B", "C"); Console.WriteLine(testGraph.distanceBetween("A", "B")); Console.WriteLine(testGraph.distanceBetween("A", "C")); testGraph.removeEdge("B", "C"); testGraph.removeEdge("B", "F"); Console.WriteLine(testGraph.distanceBetween("A", "B")); Console.WriteLine(testGraph.distanceBetween("A", "C")); Console.WriteLine(); testGraph.print(); Console.WriteLine(); Console.WriteLine(); curGraph.breadthFirstSearch("A", "D"); curGraph.depthFirstSearch("A", "D"); }
public static void Main(string[] args) { int noOfTC = Convert.ToInt32(Console.ReadLine()); for (int t = 0; t < noOfTC; ++t) { ////int num = Convert.ToInt32(Console.ReadLine()); //string[] s1 = Console.ReadLine().Split(' '); ////int[] arr = Array.ConvertAll(s1, int.Parse); ////string str = Console.ReadLine(); //int i = 0; Graph g = new Graph(5); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(3, 4); g.addEdge(4, 5); g.addEdge(5, 3); Console.WriteLine(g.HasCycle(g)); //for this graph, cycle will have [0]=3, [1]=5, [2]=4. So print cycle in reverse order to get the cycle. } Console.ReadLine(); }
private void GenerateDataRoutine(WorldState currentState, int level) { List <Action> possibleActions = currentState.getPossibleActions(); foreach (Action item in possibleActions) { WorldState ws = currentState.applyAction(item); if (!_visitedStates.Contains(ws)) { _visitedStates.Add(ws.Clone()); _graph.addEdge(currentState, ws, item); if (level - 1 <= 0) { _finalStates.Add(ws.Clone()); } else { GenerateDataRoutine(ws, level - 1); } } else { _graph.addEdge(currentState, ws, item); if (level - 1 <= 0) { _finalStates.Add(ws.Clone()); } } } }
public IEnumerator getGraphFromNode(string title) { Debug.Log("reached getGraphFromNode"); yield return(StartCoroutine(parseContentFromHTML(title))); Graph graph = new Graph(); Graph.Information info = new Graph.Information(title); Graph.Node firstNode = new Graph.Node(info); firstNode.setPosition(0.0f, 0.0f, 0.0f); graph.addNode(firstNode); //we assume that we don't go deeper here. //another approach would be to go the same route as Coroutine "getGraph" //we use this approach here because it's easier to implement and unterstand //plus the depth is unlikely to go deeper if (activeNode.getSubchapters() != null) { foreach (Graph.Information.Chapter subChapter in activeNode.getSubchapters()) { Graph.Information neighborInfo = new Graph.Information(subChapter.getTitle()); Graph.Node nextNode = new Graph.Node(neighborInfo); nextNode.setPosition(Random.Range(-DEFAULT_WIDTH / 2, DEFAULT_WIDTH / 2), Random.Range(-DEFAULT_HEIGHT / 2, DEFAULT_HEIGHT / 2), Random.Range(-DEFAULT_LENGTH / 2, DEFAULT_LENGTH / 2)); Graph.Edge nextedge = new Graph.Edge(firstNode, nextNode); graph.addNode(nextNode); graph.addEdge(nextedge); if (subChapter.getSubchapters() != null) { foreach (Graph.Information.Chapter subSubChapter in subChapter.getSubchapters()) { Graph.Information subNeighborInfo = new Graph.Information(subSubChapter.getTitle()); Graph.Node subNextNode = new Graph.Node(subNeighborInfo); subNextNode.setPosition(Random.Range(-DEFAULT_WIDTH / 2, DEFAULT_WIDTH / 2), Random.Range(-DEFAULT_HEIGHT / 2, DEFAULT_HEIGHT / 2), Random.Range(-DEFAULT_LENGTH / 2, DEFAULT_LENGTH / 2)); Graph.Edge subNextedge = new Graph.Edge(nextNode, subNextNode); graph.addNode(subNextNode); graph.addEdge(subNextedge); if (subSubChapter.getSubchapters() != null) { foreach (Graph.Information.Chapter subSubSubChapter in subChapter.getSubchapters()) { Graph.Information subSubNeighborInfo = new Graph.Information(subSubChapter.getTitle()); Graph.Node subSubNextNode = new Graph.Node(subSubNeighborInfo); subSubNextNode.setPosition(Random.Range(-DEFAULT_WIDTH / 2, DEFAULT_WIDTH / 2), Random.Range(-DEFAULT_HEIGHT / 2, DEFAULT_HEIGHT / 2), Random.Range(-DEFAULT_LENGTH / 2, DEFAULT_LENGTH / 2)); Graph.Edge subSubNextedge = new Graph.Edge(subNextNode, subSubNextNode); graph.addNode(subSubNextNode); graph.addEdge(subSubNextedge); } } } } } } setGraph(graph, 100.0f, 100.0f, 100.0f); drawGraph(); }
public static void Main(string[] args) { int noOfTC = Convert.ToInt32(Console.ReadLine()); for (int t = 0; t < noOfTC; ++t) { ////int num = Convert.ToInt32(Console.ReadLine()); //string[] s1 = Console.ReadLine().Split(' '); ////int[] arr = Array.ConvertAll(s1, int.Parse); ////string str = Console.ReadLine(); //int i = 0; Graph g = new Graph(8); g.addEdge(1, 3); g.addEdge(2, 3); g.addEdge(2, 4); g.addEdge(3, 5); g.addEdge(4, 6); g.addEdge(5, 8); g.addEdge(5, 6); g.addEdge(6, 7); Stack <int> result = g.TopologicalSort(); //print the result stack now } Console.ReadLine(); }
public void TestEdge() { Graph myGraph = new Graph(); myGraph.addVertex("001", 21); myGraph.addVertex("002", "string node"); myGraph.addVertex("003", new Uri("http://github.com")); myGraph.addEdge("Edge 1", "001", "003"); myGraph.addEdge(2, "002", "003"); myGraph.addEdge(new Random(), "003", "002"); Assert.AreEqual(true, myGraph.deleteEdge(myGraph.getVertex("001"), myGraph.getVertex("003"))); }
/** * Unit tests the {@code Bipartite} data type. * * @param args the command-line arguments */ public static void main(String[] args) { int V1 = Integer.parseInt(args[0]); int V2 = Integer.parseInt(args[1]); int E = Integer.parseInt(args[2]); int F = Integer.parseInt(args[3]); // create random bipartite graph with V1 vertices on left side, // V2 vertices on right side, and E edges; then add F random edges Graph G = GraphGenerator.bipartite(V1, V2, E); for (int i = 0; i < F; i++) { int v = StdRandom.uniform(V1 + V2); int w = StdRandom.uniform(V1 + V2); G.addEdge(v, w); } StdOut.println(G); Bipartite b = new Bipartite(G); if (b.isBipartite()) { StdOut.println("Graph is bipartite"); for (int v = 0; v < G.V(); v++) { StdOut.println(v + ": " + b.color(v)); } } else { StdOut.print("Graph has an odd-length cycle: "); for (int x : b.oddCycle()) { StdOut.print(x + " "); } StdOut.println(); } }
// Use this for initialization public void Start() { //build graph graph = new Graph<SpeechData, string>(15,false); string[] arr;// = gameObject.GetComponent<speech>().introLines; //Quest arr = new string[] { "You want a quest?", "Sure", "No", }; graph.addItem(new SpeechData(arr, "quest", true)); //Accept quest arr = new string[] { "Quest Accepted" }; graph.addItem(new SpeechData(gameObject.GetComponent<speech>().questLines, "accept", false)); //Decline quest arr = new string[] { "Quest Declined" }; graph.addItem(new SpeechData(arr, "decline", false, true)); //Close arr = new string[] { "See you later." }; graph.addItem(new SpeechData(arr, "close", false, true)); //Help arr = new string[] { "Temp help string" }; graph.addItem(new SpeechData(arr, "help", false)); //Busy arr = new string[] { "I see you're busy.", "You're already on a job.", "You can't help me if you're helping someone else.", "Sorry, the script says you can only help one person at a time." }; graph.addItem(new SpeechData(arr, "busy", false, true)); //Complain arr = new string[] { "Well you should get back on the job.", "You still don't have it!" }; graph.addItem(new SpeechData(arr, "complain", false, true)); //Congrat arr = new string[] { "Great job!", "Thanks a lot!" }; graph.addItem(new SpeechData(arr, "congrat", false, true)); //Reveal arr = new string[] { "Person has weapon format" }; graph.addItem(new SpeechData(arr, "reveal", false)); //Hope arr = new string[] { "Hope that helps.", "Glad to be of some help.", "Good Luck" }; graph.addItem(new SpeechData(arr, "hope", false, true)); arr = new string[] { "Accuse this person?", "GUILTY!!", "NOT GUILTY...YET" }; graph.addItem(new SpeechData(arr, "accuse", true)); arr = new string[] { "This person is...", "" }; graph.addItem(new SpeechData(arr, "end", false)); //Intro Lines graph.addItem(new SpeechData(gameObject.GetComponent<speech>().introLines, "intro", false)); curNode = graph.findNode("intro"); //Passive Lines graph.addItem(new SpeechData(gameObject.GetComponent<speech>().passiveLines, "passive", false, true)); //edges graph.addEdge("intro", "accuse", 0); graph.addEdge("accuse", "end", 0); graph.addEdge("accuse", "passive", 1); graph.addEdge("passive", "quest", 0); graph.addEdge("passive", "help", 1); graph.addEdge("passive", "busy", 2); graph.addEdge("passive", "complain", 3); graph.addEdge("passive", "congrat", 4); graph.addEdge("help", "close", 0); graph.addEdge("busy", "close", 0); graph.addEdge("complain", "close", 0); //CHANGE UNDER LATER************************** graph.addEdge("congrat", "reveal", 0); graph.addEdge("reveal", "hope", 0); graph.addEdge("hope", "close", 0); graph.addEdge("quest", "accept", 0); graph.addEdge("quest", "decline", 1); graph.addEdge("accept", "close", 0); graph.addEdge("decline", "close", 0); }
} // ensure non-instantiability. /// <summary> Creates a new edge and adds it to the specified graph similarly to the /// {@link Graph#addEdge(Object, Object)} method. /// /// </summary> /// <param name="g">the graph for which the edge to be added. /// </param> /// <param name="sourceVertex">source vertex of the edge. /// </param> /// <param name="targetVertex">target vertex of the edge. /// </param> /// <param name="weight">weight of the edge. /// /// </param> /// <returns> The newly created edge if added to the graph, otherwise /// <code>null</code>. /// /// </returns> /// <seealso cref="Graph.addEdge(Object, Object)"> /// </seealso> public static Edge addEdge(Graph g, System.Object sourceVertex, System.Object targetVertex, double weight) { EdgeFactory ef = g.EdgeFactory; Edge e = ef.createEdge(sourceVertex, targetVertex); // we first create the edge and set the weight to make sure that // listeners will see the correct weight upon addEdge. e.Weight = weight; return g.addEdge(e)?e:null; }
/// <summary> Adds the specified source and target vertices to the graph, if not /// already included, and creates a new edge and adds it to the specified /// graph similarly to the {@link Graph#addEdge(Object, Object)} method. /// /// </summary> /// <param name="g">the graph for which the specified edge to be added. /// </param> /// <param name="sourceVertex">source vertex of the edge. /// </param> /// <param name="targetVertex">target vertex of the edge. /// /// </param> /// <returns> The newly created edge if added to the graph, otherwise /// <code>null</code>. /// </returns> public static Edge addEdgeWithVertices(Graph g, System.Object sourceVertex, System.Object targetVertex) { g.addVertex(sourceVertex); g.addVertex(targetVertex); return g.addEdge(sourceVertex, targetVertex); }
/// <summary> Adds the specified edge to the specified graph including its vertices. /// If any of the vertices of the specified edge are not already in the /// graph they are also added (before the edge is added). /// /// </summary> /// <param name="g">the graph for which the specified edge to be added. /// </param> /// <param name="e">the edge to be added to the graph (including its vertices). /// /// </param> /// <returns> <code>true</code> if and only if the specified edge was not /// already contained in the graph. /// </returns> public static bool addEdgeWithVertices(Graph g, Edge e) { g.addVertex(e.Source); g.addVertex(e.Target); return g.addEdge(e); }
public void generateGraph(int rows, int columns, String[] inp) { int num_nodes = 0; String key; char[] charArray, charArray2; for (int i = 1; i < rows-1; i++) { charArray = inp[i].ToCharArray(); for (int j = 1; j < columns-1; j++) { if (charArray[j] != '#') { key = i + "," + j; h.Add(key, num_nodes); if (charArray[j] == 'S') { start_node = num_nodes; } if (charArray[j] == 'E') { end_node = num_nodes; } num_nodes++; } } } g = new Graph<Square>(num_nodes); int x, y; //Matrica na sosednost for (int i = 1; i < rows - 1; i++) { charArray = inp[i].ToCharArray(); for (int j = 1; j < columns - 1; j++) { if (charArray[j] != '#') { if (charArray[j - 1] != '#') { h.TryGetValue(i + "," + j, out x); h.TryGetValue(i + "," + (j-1), out y); g.addEdge(x, y); } if (charArray[j + 1] != '#') { h.TryGetValue(i + "," + j, out x); h.TryGetValue(i + "," + (j + 1), out y); g.addEdge(x, y); } charArray2 = inp[i - 1].ToCharArray(); if (charArray2[j] != '#') { h.TryGetValue(i + "," + j, out x); h.TryGetValue((i-1) + "," + j, out y); g.addEdge(x, y); } charArray2 = inp[i + 1].ToCharArray(); if (charArray2[j] != '#') { h.TryGetValue(i + "," + j, out x); h.TryGetValue((i+1) + "," + j, out y); g.addEdge(x, y); } } } } }
public Graph parse() { Graph graph = new Graph(); string line; Random rng = new Random(); // Read the file and line by line and fill the graph. System.IO.StreamReader file = new System.IO.StreamReader(filepath); if (file == null) { Console.WriteLine ("file not found"); return null; } while((line = file.ReadLine()) != null) { if(line.Equals(" node")) { int id; string sid; string name; string[] words; char[] separatingChars = { ' ' }; file.ReadLine(); // [ sid = file.ReadLine(); // id ie. id 0 words = sid.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries ); id = Int32.Parse(words[1]); name = file.ReadLine(); // name ie. lable "Beak" words = name.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries ); name = words[1]; graph.addNode(new Graph.Node(id, name, rng.Next(-100, 100), rng.Next(-100, 100), rng.Next(-100, 100))); } else if(line.Equals(" edge")) { int target, source; string s; string[] words; char[] separatingChars = { ' ' }; file.ReadLine(); // [ s = file.ReadLine(); // source ie. source 19 words = s.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries ); source = Int32.Parse(words[1]); s = file.ReadLine(); // target ie. target 1 words = s.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries ); target = Int32.Parse(words[1]); graph.addEdge(source, target); } } file.Close(); return graph; }
//Will create the graph treating all reachable areas as nodes private void createGraph() { currentGraph = new Graph(); int height = boolTiles.GetLength (0); int width = boolTiles.GetLength (1); //Here is where we form our graph //First we add all of the nodes for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (boolTiles[i, j] == true) { currentGraph.addNode(new Node(i, j)); } } } //Now we add all of our connections foreach(Node node in currentGraph.nodes.Values) { int nodeHeight = node.heightPos; int nodeWidth = node.widthPos; bool isUp = nodeHeight > 0; bool isRight = nodeWidth < (width - 1); bool isDown = nodeHeight < (height - 1); bool isLeft = nodeWidth > 0; /* //We need to check upper left pos if (isUp && isLeft && boolTiles[nodeHeight - 1, nodeWidth - 1]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight - 1, nodeWidth - 1)); } */ //We need to check upper pos if (isUp && boolTiles[nodeHeight - 1, nodeWidth]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight - 1, nodeWidth)); } /* //We need to check upper right pos if (isUp && isRight && boolTiles[nodeHeight - 1, nodeWidth + 1]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight - 1, nodeWidth + 1)); } */ //We need to check right pos if (isRight && boolTiles[nodeHeight, nodeWidth + 1]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight, nodeWidth + 1)); } /* //We need to check lower right pos if (isDown && isRight && boolTiles[nodeHeight + 1, nodeWidth + 1]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight + 1, nodeWidth + 1)); } */ //We need to check lower pos if (isDown && boolTiles[nodeHeight + 1, nodeWidth]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight + 1, nodeWidth)); } /* //We need to check lower left pos if (isDown && isLeft && boolTiles[nodeHeight + 1, nodeWidth - 1]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight + 1, nodeWidth - 1)); } */ //We need to check left pos if (isLeft && boolTiles[nodeHeight, nodeWidth - 1]) { currentGraph.addEdge(node, currentGraph.getNodeByLocation(nodeHeight, nodeWidth - 1)); } } }
public AddEdgeCommand(Graph g, Vertex v1, Vertex v2) { mementos.Add(g.getMemento()); _edge = g.addEdge(v1, v2); }