static void Main(string[] args) { budget = int.Parse(Console.ReadLine().Split(' ')[1]); nodes = int.Parse(Console.ReadLine().Split(' ')[1]); edges = int.Parse(Console.ReadLine().Split(' ')[1]); GraphClass graph = new GraphClass(nodes); for (int i = 0; i < edges; i++) { var line = Console.ReadLine().Split(' '); int startNode = int.Parse(line[0]); int endNode = int.Parse(line[1]); int weight = int.Parse(line[2]); graph.AddEdge(new Edge(startNode, endNode, weight)); if (line.Length == 4) { graph.Connect(startNode, endNode, 0); } } Console.WriteLine("Budget used: {0}", Calc(Solve(graph, nodes, budget))); }
private void Testing(ClassEnum classEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[classEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(classEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); GraphClass.GraphClassEnum graphClassEnum = GraphClass.GetGraphClass(graph); stringBuilder.AppendLine("Graph class: " + graphClassEnum.ToString()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(classEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
public bool saveGraphToFile(GraphClass graph) { // saving to user named file and user selected dir Stream TestFileStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Graph file (*.gph)|*.gph"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((TestFileStream = saveFileDialog1.OpenFile()) != null) { BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(TestFileStream, graph); TestFileStream.Close(); } else { return false; } } return true; }
public GraphClass loadGraphFromFile() { // load from user file Stream TestFileStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); GraphClass graph = new GraphClass(); openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "Graph file (*.gph)|*.gph"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { if ((TestFileStream = openFileDialog1.OpenFile()) != null) { BinaryFormatter deserializer = new BinaryFormatter(); graph = (GraphClass)deserializer.Deserialize(TestFileStream); TestFileStream.Close(); return graph; } } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } return graph; }
private Create(TypeExpr token, GraphClass graphClass, OrientSqlExpr @from, OrientSqlExpr @to, OrientSqlExpr set) { _token = token; _graphClass = graphClass; _from = @from; _to = to; _set = set; }
public AgeichenkoTestTask() { InitializeComponent(); stateOfForm = stateEnum.stateNodeAdding; paintBox = pictureBoxGraph.CreateGraphics(); graph = new GraphClass(); sqlHandler = new SqlHandlerClass(); serializer = new SerializeHandlerClass(); notifiers = new List<ToolTip>(); updatePaintingSettings(); currentLanguage = "en"; switchLanguage(currentLanguage); }
public ArrayList findShortWay(NodeClass fromThis, NodeClass toThis, GraphClass graph) { // Dijkstra's algorithm // d - minimum weights of ways to other nodes // u - marks of visited nodes // p - parent of each node, its necessary for back way finding d[graph.findNodeIndexByNodeNumber(fromThis.NodeNumber)] = 0; for (int i = 0; i < sizeOfNodes; ++i) { // weights to every node is infinite from the start int v = -1; for (int j = 0; j < sizeOfNodes; ++j) if (!u[j] && (v == -1 || d[j] < d[v])) v = j; if (d[v] == 999999999) break; u[v] = true; for (int j = 0; j < graph.GraphNodes[v].nodeEdges.Count(); ++j) { // if current way have lower weight sum to the next node - set new weight and parent int to = graph.findNodeIndexByNodeNumber(graph.GraphNodes[v].nodeEdges[j].NextNode.NodeNumber); int len = graph.GraphNodes[v].nodeEdges[j].Weight; if (d[v] + len < d[to]) { d[to] = d[v] + len; p[to] = v; } } } // find a way back from end to start and reverse it ArrayList path = new ArrayList(); // i have no idea, how index v could be less than 0, but it has happened a few times // programming is... magic! for (int v = graph.findNodeIndexByNodeNumber(toThis.NodeNumber); (v != graph.findNodeIndexByNodeNumber( fromThis.NodeNumber )) && (v >= 0) && (p[v] >= 0); v = p[v]) path.Add(v); path.Add(graph.findNodeIndexByNodeNumber( fromThis.NodeNumber )); path.Reverse(); return path; }
private static List <Edge> Solve(GraphClass graph, int nodes, int budget) { List <Edge> result = new List <Edge>(); graph.Edges.Sort(); foreach (var edge in graph.Edges) { if (budget > edge.Weight) { if (!graph.IsConnected(edge.StartNode, edge.EndNode)) { if (graph.Connect(edge.StartNode, edge.EndNode, edge.Weight)) { budget -= edge.Weight; result.Add(edge); } } } } return(result); }
public Create(TypeExpr edge, GraphClass graphClass, From @from, To to, Set set) : this(edge, graphClass, @from as OrientSqlExpr, to, set) { }
public Create(TypeExpr vertex, GraphClass graphClass, Set set) : this(vertex, graphClass, new None(), new None(), set as OrientSqlExpr) { }
public bool loadGraphFromDB(GraphClass graph) { int EdgeId = 0; int EdgeParent = 0; int EdgeDestination = 0; int NodeId = -1; int NodeX = -1; int NodeY = -1; //read data try { sqlConnect.Open(); sqlCommand.Connection = sqlConnect; } catch (Exception ex) { MessageBox.Show("Error: Could not open database. Original error: " + ex.Message); return false; } graph.clearGraph(); // read nodes sqlCommand.CommandText = "select * from Nodes"; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows) { while (sqlDataReader.Read()) { NodeId = -1; NodeX = -1; NodeY = -1; NodeId = Convert.ToInt32(sqlDataReader[0].ToString()); NodeX = Convert.ToInt32(sqlDataReader[1].ToString()); NodeY = Convert.ToInt32(sqlDataReader[2].ToString()); if (NodeX > 0 && NodeY > 0) { Point position = new Point(NodeX, NodeY); graph.addNode(position, NodeId); } else { MessageBox.Show("Error: Database doesn't have any nodes"); } } } sqlDataReader.Close(); // read Edges sqlCommand.CommandText = "select * from Edges"; sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows) { while (sqlDataReader.Read()) { EdgeId = Convert.ToInt32(sqlDataReader[0].ToString()); EdgeParent = Convert.ToInt32(sqlDataReader[1].ToString()); EdgeDestination = Convert.ToInt32(sqlDataReader[2].ToString()); NodeClass parentNode = null; NodeClass destinationNode = null; parentNode = graph.findNodeByNodeNumber(EdgeParent); destinationNode = graph.findNodeByNodeNumber(EdgeDestination); if (parentNode != null && destinationNode != null) { graph.addEdge(parentNode, destinationNode); } else { MessageBox.Show("Error: Database doesn't have any edges"); } } } sqlDataReader.Close(); sqlConnect.Close(); //end of read data graph.drawGraph(); return true; }
public bool saveGraphToDB(GraphClass graph) { int EdgeId = 0; int EdgeParent = 0; int EdgeDestination = 0; int NodeId = 0; int NodeX = 0; int NodeY = 0; //string connStr = @"Data Source=(LocalDB)\v11.0;Initial Catalog=GraphDBv11;Integrated Security=True"; // is this data base exist? sqlConnect = new SqlConnection(connStr); try { sqlConnect.Open(); sqlCommand.Connection = sqlConnect; } catch(Exception ex) { MessageBox.Show("Error: Could not open database. Original error: " + ex.Message); return false; } //clear all data sqlCommand.CommandText = "delete from Nodes"; sqlCommand.ExecuteNonQuery(); sqlCommand.CommandText = "delete from Edges"; sqlCommand.ExecuteNonQuery(); //insert new data try { foreach (NodeClass node in graph.GraphNodes) { NodeId = node.NodeNumber; NodeX = node.NodePosition.X; NodeY = node.NodePosition.Y; sqlCommand.CommandText = "insert into Nodes (NodeId, NodeX, NodeY) " + "values ('" + NodeId + "','" + NodeX + "','" + NodeY + "')"; sqlCommand.ExecuteNonQuery(); foreach (EdgeClass edge in node.nodeEdges) { EdgeParent = node.NodeNumber; EdgeDestination = edge.NextNode.NodeNumber; sqlCommand.CommandText = "insert into Edges (EdgeId, EdgeParent, EdgeDestination) " + "values ('" + EdgeId++ + "','" + EdgeParent + "','" + EdgeDestination + "')"; sqlCommand.ExecuteNonQuery(); } } } catch (Exception ex) { MessageBox.Show("Error: Could not insert data into database. Original error: " + ex.Message); } sqlConnect.Close(); // end of insert return true; }
// load from file private void fromFileToolStripMenuItem_Click(object sender, EventArgs e) { GraphClass newGraph = null; newGraph = serializer.loadGraphFromFile(); if (newGraph != null) { graph = newGraph; updatePaintingSettings(); graph.drawGraph(); infoUpdate("InfoSuccessLoadS"); } else { infoUpdate("InfoFailLoadS"); } }