public void SimpleEqualsTest(bool directed) { Graph g = new MatrixGraph(7, directed); g.AddEdge(0, 1, 5); g.AddEdge(0, 2, 0); g.AddEdge(0, 3, 3); g.AddEdge(1, 5, 3); g.AddEdge(1, 6, 4); g.AddEdge(2, 3, 2); g.AddEdge(2, 6, 15); g.AddEdge(3, 5, 2); g.AddEdge(3, 4, 1); g.AddEdge(4, 5, 12); g.AddEdge(5, 6, 3); Graph g2 = new MatrixGraph(7, directed); g2.AddEdge(0, 1, 5); g2.AddEdge(0, 2, 0); g2.AddEdge(0, 3, 3); g2.AddEdge(1, 5, 3); g2.AddEdge(1, 6, 4); g2.AddEdge(2, 3, 2); g2.AddEdge(2, 6, 15); g2.AddEdge(3, 5, 2); g2.AddEdge(3, 4, 1); g2.AddEdge(4, 5, 12); g2.AddEdge(5, 6, 3); bool result = g.Equals(g2); Assert.True(result); }
static void Main(string[] args) { DateTime time = DateTime.Now; Console.WriteLine(time); IGraph g = new ListGraph(100, 1000); string[] a = g.Neighbours("w1"); for (int i = 0; i < a.Length; i++) { if (a[i] != "") { //Console.WriteLine(a[i]); } } IGraph graph = new MatrixGraph(100, 1000); BFS poszukaj = new BFS(); string[] wynik = poszukaj.FindWay(graph, "w1", "w2"); for (int i = 0; i < wynik.Length; i++) { Console.WriteLine(wynik[i]); } Console.WriteLine(time - DateTime.Now); }
public void SimpleBellmanFordTest(bool directed) { Graph g = new MatrixGraph(7, directed); g.AddEdge(0, 1, 5); g.AddEdge(0, 2, 3); g.AddEdge(0, 3, 0); g.AddEdge(1, 4, 3); g.AddEdge(1, 6, 4); g.AddEdge(2, 4, 2); g.AddEdge(2, 5, 1); g.AddEdge(3, 2, 2); g.AddEdge(3, 6, 15); g.AddEdge(4, 6, 3); g.AddEdge(5, 4, 12); var result = g.BellmanFord(0); Assert.Equal(0, result[0].distance); Assert.Equal(5, result[1].distance); Assert.Equal(2, result[2].distance); Assert.Equal(0, result[3].distance); Assert.Equal(4, result[4].distance); Assert.Equal(3, result[5].distance); Assert.Equal(7, result[6].distance); }
public string GetResult() { Graph graph1 = new ListGraph(); Graph graph2 = new MatrixGraph(); return($"Graph1: {graph1.GetName()}\nGraph2: {graph2.GetName()}"); }
public void TestGetCutPoints1() { MatrixGraph graph = new MatrixGraph(9, 10); graph.SetBidEdge(0, 1, 1); graph.SetBidEdge(1, 2, 1); graph.SetBidEdge(2, 0, 1); graph.SetBidEdge(1, 3, 1); graph.SetBidEdge(1, 4, 1); graph.SetBidEdge(3, 4, 1); graph.SetBidEdge(3, 5, 1); graph.SetBidEdge(5, 6, 1); graph.SetBidEdge(5, 7, 1); graph.SetBidEdge(5, 8, 1); graph.SetBidEdge(6, 7, 1); graph.SetBidEdge(7, 8, 1); HashSet <int> cutPoints = graph.GetCutPoints(); Assert.AreEqual(4, cutPoints.Count); Assert.IsTrue(cutPoints.Contains(0)); Assert.IsTrue(cutPoints.Contains(1)); Assert.IsTrue(cutPoints.Contains(3)); Assert.IsTrue(cutPoints.Contains(5)); }
public void SimpleDFSTest(bool value) { Random random = new Random(); Graph g = new MatrixGraph(9, value); g.AddEdge(0, 1, random.NextDouble()); g.AddEdge(0, 2, random.NextDouble()); g.AddEdge(0, 3, random.NextDouble()); g.AddEdge(1, 4, random.NextDouble()); g.AddEdge(1, 6, random.NextDouble()); g.AddEdge(2, 4, random.NextDouble()); g.AddEdge(2, 5, random.NextDouble()); g.AddEdge(3, 7, random.NextDouble()); g.AddEdge(4, 7, random.NextDouble()); g.AddEdge(6, 7, random.NextDouble()); g.AddEdge(5, 8, random.NextDouble()); bool[] tab; g.DFS(null, null, null, 0, out tab); foreach (bool b in tab) { Assert.True(b); } bool[] invTab; Graph invGraph = g.ReversedGraph(); invGraph.DFS(null, null, null, 0, out invTab); Assert.True(invTab[0]); for (int i = 1; i < invTab.Length; i++) { Assert.Equal(!value, invTab[i]); } }
public void CreateSimpleDirectedGraph(int value) { Graph graph = new MatrixGraph(value, true); Assert.Equal(value, graph.VerticesCount); Assert.True(graph.Directed); }
public void SimpleKrusalTest() { Graph g = new MatrixGraph(7, false); g.AddEdge(0, 1, 5); g.AddEdge(0, 2, 3); g.AddEdge(0, 3, 0); g.AddEdge(1, 4, 3); g.AddEdge(1, 6, 4); g.AddEdge(2, 4, 2); g.AddEdge(2, 5, 1); g.AddEdge(3, 2, 2); g.AddEdge(3, 6, 15); g.AddEdge(4, 6, 3); g.AddEdge(5, 4, 12); var result = g.Kruskal(); Assert.True(result.connected); Assert.Equal(0, result.tree.GetEdge(0, 3).Weight); Assert.Equal(2, result.tree.GetEdge(3, 2).Weight); Assert.Equal(1, result.tree.GetEdge(2, 5).Weight); Assert.Equal(2, result.tree.GetEdge(2, 4).Weight); Assert.Equal(3, result.tree.GetEdge(1, 4).Weight); Assert.Equal(3, result.tree.GetEdge(4, 6).Weight); Assert.Equal(6, result.tree.EdgesCount); }
public static void Main() { MatrixGraph mg = new MatrixGraph(); mg.generate_random(5, 3); Console.WriteLine(mg); }
public void TestArrayHeapDijkstra() { Random ran = new Random(); MatrixGraph graph = new MatrixGraph(100, 101); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { graph.SetUniEdge(i, j, ran.Next(100)); } graph.SetUniEdge(i, i, 0); } double[][] floyd = graph.Floyd(); for (int i = 0; i < 100; i++) { double[] dijkstra = graph.Dijkstra(i, new ArrayHeap <VertexNode>()); for (int j = 0; j < 100; j++) { if (floyd[i][j] != dijkstra[j]) { Console.WriteLine("i:{0} j:{1} floyd:{2} dijkstra:{3}", i, j, floyd[i][j], dijkstra[j]); } Assert.AreEqual(floyd[i][j], dijkstra[j]); } } }
public void TestMatrixGraph() { var emptyGraph = new MatrixGraph<int>(); var root = new GraphNode<int>(10); var expandedGraph = root.MakeEdges<int>(null); }
public void SimpleDijkstraTest(bool directed) { Graph g = new MatrixGraph(7, directed); g.AddEdge(0, 1, 5); g.AddEdge(0, 2, 0); g.AddEdge(0, 3, 3); g.AddEdge(1, 5, 3); g.AddEdge(1, 6, 4); g.AddEdge(2, 3, 2); g.AddEdge(2, 6, 15); g.AddEdge(3, 5, 2); g.AddEdge(3, 4, 1); g.AddEdge(4, 5, 12); g.AddEdge(5, 6, 3); var result = g.DAG(0); Assert.Equal(0, result[0].distance); Assert.Equal(5, result[1].distance); Assert.Equal(0, result[2].distance); Assert.Equal(2, result[3].distance); Assert.Equal(3, result[4].distance); Assert.Equal(4, result[5].distance); Assert.Equal(7, result[6].distance); }
public void TestOneEdgeDijkstra() { MatrixGraph graph = new MatrixGraph(2, 10); graph.SetUniEdge(0, 1, 2); double[] result = graph.Dijkstra(0); Assert.AreEqual(0, result[0]); Assert.AreEqual(2, result[1]); }
public void TestOneEdgeFloyd() { MatrixGraph graph = new MatrixGraph(2, 10); graph.SetUniEdge(0, 1, 2); double[][] result = graph.Floyd(); Assert.AreEqual(10, result[1][0]); Assert.AreEqual(2, result[0][1]); }
public void SimpleFailEqualsTest(bool directed) { Graph g = new MatrixGraph(7, directed); Graph g2 = new MatrixGraph(7, !directed); bool result = g.Equals(g2); Assert.False(result); }
private void clearAllAndCreateNewGraph(bool graphUploaded) { graph = null; if (!graphUploaded) { graph = new MatrixGraph((uint)numberOfVertices.Value); } listOfEgdes.Items.Clear(); resultLabel.Text = "?"; graphImage.Image = null; }
public void TestNormalFloyd() { MatrixGraph graph = new MatrixGraph(3, 10); graph.SetUniEdge(0, 1, 2); graph.SetUniEdge(1, 2, 5); graph.SetUniEdge(0, 2, 6); double[][] result = graph.Floyd(); Assert.AreEqual(2, result[0][1]); Assert.AreEqual(6, result[0][2]); Assert.AreEqual(5, result[1][2]); }
public void ReverseGraph() { Graph graph = new MatrixGraph(10, true); graph.AddEdge(0, 9, 10); graph.AddEdge(2, 0, 5); Graph reversedGraph = graph.ReversedGraph(); Assert.Equal(1, reversedGraph.GetOutDegree(9)); Assert.Equal(1, reversedGraph.GetInDegree(2)); Assert.Equal(2, reversedGraph.EdgesCount); }
public void IsolatedGraph(bool value) { Graph graph = new MatrixGraph(10, true); graph.AddEdge(0, 9, 10); graph.AddEdge(2, 0, 5); Graph isolatedGraph = graph.IsolatedGraph(value); Assert.Equal(10, isolatedGraph.VerticesCount); Assert.Equal(value, isolatedGraph.Directed); Assert.Equal(0, isolatedGraph.EdgesCount); }
public void GraphOfSameType(bool value) { Graph graph = new MatrixGraph(10, true); graph.AddEdge(0, 9, 10); graph.AddEdge(2, 0, 5); Graph sameTypeGraph = graph.NewGraphOfSameType(5, value); Assert.Equal(5, sameTypeGraph.VerticesCount); Assert.Equal(value, sameTypeGraph.Directed); Assert.Equal(0, sameTypeGraph.EdgesCount); Assert.IsType <MatrixGraph>(sameTypeGraph); }
public void UndirectedGraphAddEdgeAndDeleteEdge() { Graph graph = new MatrixGraph(10, false); graph.AddEdge(0, 9, 10); graph.AddEdge(2, 0, 5); Assert.Equal(2, graph.GetOutDegree(0)); Assert.Equal(2, graph.GetInDegree(0)); Assert.Equal(2, graph.EdgesCount); graph.DeleteEdge(0, 9); Assert.Equal(1, graph.GetOutDegree(0)); Assert.Equal(1, graph.GetInDegree(0)); Assert.Equal(1, graph.EdgesCount); }
public void TestUndirected() { var graph = new MatrixGraph(GraphTypes.Undirected, 5); graph.AddEdge(0, 1); graph.AddEdge(0, 4); graph.AddEdge(1, 4); graph.AddEdge(1, 3); graph.AddEdge(1, 2); graph.AddEdge(2, 3); graph.AddEdge(3, 4); var print = graph.PrintBFS(3); }
public void TestDirected() { var graph = new MatrixGraph(GraphTypes.Directed, 6); graph.AddEdge(0, 1); graph.AddEdge(0, 4); graph.AddEdge(1, 2); graph.AddEdge(2, 0); graph.AddEdge(2, 3); graph.AddEdge(3, 1); graph.AddEdge(3, 4); graph.AddEdge(3, 5); graph.AddEdge(4, 1); graph.AddEdge(5, 0); var print = graph.PrintBFS(3); }
public void TestDirected_DFS() { var graph = new MatrixGraph(GraphTypes.Directed, 7); graph.AddEdge(0, 1); graph.AddEdge(0, 3); graph.AddEdge(1, 2); graph.AddEdge(1, 3); graph.AddEdge(1, 4); graph.AddEdge(2, 0); graph.AddEdge(2, 4); graph.AddEdge(3, 2); graph.AddEdge(4, 5); graph.AddEdge(5, 6); graph.AddEdge(6, 3); var print = graph.PrintDFS(0); }
private void addEdgeButton_Click(object sender, EventArgs e) { if (edgeFrom.Value == edgeTo.Value) { return; } string newItem = edgeFrom.Value.ToString() + "<------>" + edgeTo.Value.ToString(); string reverseItem = edgeTo.Value.ToString() + "<------>" + edgeFrom.Value.ToString(); if (!listOfEgdes.Items.Contains(newItem) && !listOfEgdes.Items.Contains(reverseItem)) { listOfEgdes.Items.Add(newItem); if (graph == null) { graph = new MatrixGraph((uint)numberOfVertices.Value); } graph.AddEdge(new Edge((int)edgeFrom.Value, (int)edgeTo.Value)); } }
public void TestBreadthFirstSearch() { MatrixGraph graph = new MatrixGraph(4, 10); graph.SetUniEdge(0, 1, 2); graph.SetUniEdge(1, 2, 5); graph.SetUniEdge(0, 2, 6); graph.SetUniEdge(1, 3, 2); graph.SetUniEdge(2, 3, 2); graph.SetUniEdge(3, 2, 2); graph.BreadthFirstSearch(0); Console.WriteLine(); graph.BreadthFirstSearch(1); Console.WriteLine(); graph.BreadthFirstSearch(2); Console.WriteLine(); graph.BreadthFirstSearch(3); }
public void TestNormalDijkstra() { MatrixGraph graph = new MatrixGraph(3, 10); graph.SetUniEdge(0, 1, 2); graph.SetUniEdge(1, 2, 5); graph.SetUniEdge(0, 2, 6); double[] result = graph.Dijkstra(0); Assert.AreEqual(0, result[0]); Assert.AreEqual(2, result[1]); Assert.AreEqual(6, result[2]); result = graph.Dijkstra(1); Assert.AreEqual(10, result[0]); Assert.AreEqual(0, result[1]); Assert.AreEqual(5, result[2]); result = graph.Dijkstra(2); Assert.AreEqual(10, result[0]); Assert.AreEqual(10, result[1]); Assert.AreEqual(0, result[2]); }
private void uploadGraphButton_Click(object sender, EventArgs e) { clearAllAndCreateNewGraph(true); OpenFileDialog theDialog = new OpenFileDialog { Title = @"Open Text File", Filter = @"TXT files|*.txt", InitialDirectory = Directory.GetCurrentDirectory(), RestoreDirectory = true }; if (theDialog.ShowDialog() == DialogResult.OK) { string filename = theDialog.FileName; int numOfVertices = 0; int[,] edges = null; try { numOfVertices = new Parser().Parse(filename, out edges); } catch (Exception) { MessageBox.Show("Wrong file format!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); graph = null; return; } //graph construction graph = new MatrixGraph((uint)numOfVertices); numberOfVertices.Value = numOfVertices; for (int i = 0; i < edges.GetLength(0); i++) { graph.AddEdge(new Edge(edges[i, 0], edges[i, 1])); listOfEgdes.Items.Add("" + edges[i, 0].ToString() + "<------>" + edges[i, 1].ToString()); } } }
public BackToInputFromDrawButton(AdjacencyList adjacencyList, List <VertexDraw> vertexDraws, List <EdgeDraw> edgeDraws, StartForm.DrawForm drawForm, AdjacencyListPanel adjacencyListPanel, WeightTable weightTable, List <List <CellBox> > matrix, List <List <CellAdjacencyList> > cells, InputCountVertexForm inputCountVertexForm, MatrixGraph matrixGraph, StartForm.StartForm startForm, string buttonText = "Matrix") : base(buttonText) { ForeColor = Color.Black; this.BackColor = Color.Orange; Font = new System.Drawing.Font("Comic Sans MS", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204))); FlatStyle = System.Windows.Forms.FlatStyle.Popup; Text = buttonText; Location = new System.Drawing.Point(400, 410); this.adjacencyList = adjacencyList; this.vertexDraws = vertexDraws; this.edgeDraws = edgeDraws; this.drawForm = drawForm; this.weightTable = weightTable; this.adjacencyListPanel = adjacencyListPanel; this.matrix = matrix; this.cells = cells; this.inputCountVertexForm = inputCountVertexForm; this.matrixGraph = matrixGraph; this.startForm = startForm; }
public void FloydWarshallTest(bool directed) { Graph g = new MatrixGraph(7, directed); g.AddEdge(0, 1, 5); g.AddEdge(0, 2, 3); g.AddEdge(0, 3, 0); g.AddEdge(1, 4, 3); g.AddEdge(1, 6, 4); g.AddEdge(2, 4, 2); g.AddEdge(2, 5, 1); g.AddEdge(3, 2, 2); g.AddEdge(3, 6, 15); g.AddEdge(4, 6, 3); g.AddEdge(5, 4, 12); var result = g.Johnson(); for (int i = 0; i < g.VerticesCount; i++) { Assert.Equal(0, result[i][i]); } Assert.Equal(5, result[0][1]); Assert.Equal(2, result[0][2]); Assert.Equal(0, result[0][3]); Assert.Equal(4, result[0][4]); Assert.Equal(3, result[0][5]); Assert.Equal(7, result[0][6]); Assert.Equal(double.MaxValue, result[2][0]); Assert.Equal(double.MaxValue, result[2][1]); Assert.Equal(double.MaxValue, result[2][3]); Assert.Equal(2, result[2][4]); Assert.Equal(1, result[2][5]); Assert.Equal(5, result[2][6]); Assert.Equal(double.MaxValue, result[2][0]); Assert.Equal(double.MaxValue, result[2][1]); Assert.Equal(double.MaxValue, result[2][3]); Assert.Equal(2, result[2][4]); Assert.Equal(1, result[2][5]); Assert.Equal(5, result[2][6]); Assert.Equal(double.MaxValue, result[3][0]); Assert.Equal(double.MaxValue, result[3][1]); Assert.Equal(2, result[3][2]); Assert.Equal(4, result[3][4]); Assert.Equal(3, result[3][5]); Assert.Equal(7, result[3][6]); Assert.Equal(double.MaxValue, result[4][0]); Assert.Equal(double.MaxValue, result[4][1]); Assert.Equal(double.MaxValue, result[4][2]); Assert.Equal(double.MaxValue, result[4][3]); Assert.Equal(double.MaxValue, result[4][5]); Assert.Equal(3, result[4][6]); Assert.Equal(double.MaxValue, result[5][0]); Assert.Equal(double.MaxValue, result[5][1]); Assert.Equal(double.MaxValue, result[5][2]); Assert.Equal(double.MaxValue, result[5][3]); Assert.Equal(12, result[5][4]); Assert.Equal(15, result[5][6]); Assert.Equal(double.MaxValue, result[6][0]); Assert.Equal(double.MaxValue, result[6][1]); Assert.Equal(double.MaxValue, result[6][2]); Assert.Equal(double.MaxValue, result[6][3]); Assert.Equal(double.MaxValue, result[6][4]); Assert.Equal(double.MaxValue, result[6][5]); }
public DrawForm(List <VertexDraw> vertexDraws, List <EdgeDraw> edgeDraws, List <List <CellBox> > matrix, StartForm startForm, InputCountVertexForm inputCountVertexForm, MatrixGraph matrixGraph, AdjacencyList adjacencyList) { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; Text = "GraphVizualizer / Draw"; this.BackColor = Color.DarkGray; DoubleBuffered = true; this.vertexDraws = vertexDraws; this.edgeDraws = edgeDraws; collisionVertex = new CollisionVertex(); newEdgeDefinition = new NewEdgeDefinition(); drawingEdges = new DrawingEdges(); MouseDown += new MouseEventHandler(MouseClickDrawForm); vertexBrush = new SolidBrush(Color.Black); vertexStringFormat = new StringFormat(); vertexStringFormat.Alignment = StringAlignment.Center; vertexStringFormat.LineAlignment = StringAlignment.Center; vertexTextFont = new Font("Times New Roman", 12, FontStyle.Bold); pen = new Pen(Brushes.Black, 5); weightTable = new WeightTable(200, 200, Size.Width - 200, 0); matrixWeightPanel = new MatrixWeightPanel(weightTable, matrix); matrixWeightPanel.DrawingMatrix(); Controls.Add(weightTable); converter = new Converter(); this.adjacencyList = adjacencyList; adListPanel = new AdjacencyListPanel(200, 200, Size.Width - 200, 0, adjacencyList); Controls.Add(adListPanel); Controls.Add(adListPanel); shortestPathPanel = new ShortestPathPanel(200, 200, 150, 0, adjacencyList, edgeDraws, this); Controls.Add(shortestPathPanel); toolPanel = new ToolPanel(0, 100, weightTable, this.edgeDraws , adjacencyList, this, adListPanel, this.vertexDraws, matrix , adListPanel.AdListTable.Cells, shortestPathPanel); Controls.Add(toolPanel); pen.EndCap = LineCap.ArrowAnchor; arrow = new Arrow(); brushes = new Brush[2]; brushes[(int)BrushColor.Orange] = Brushes.Orange; brushes[(int)BrushColor.Black] = Brushes.Black; backToMenuOfDrawButton = new BackToMenuFromDrawButton(adjacencyList, vertexDraws, edgeDraws, this, adListPanel, weightTable, matrix, adListPanel.AdListTable.Cells); backToInputFromDrawButton = new BackToInputFromDrawButton(adjacencyList, vertexDraws, edgeDraws, this, adListPanel, weightTable, matrix, adListPanel.AdListTable.Cells, inputCountVertexForm, matrixGraph, startForm); Controls.Add(backToInputFromDrawButton); Controls.Add(backToMenuOfDrawButton); }