/// <summary> /// Serialize this object's properties to XML. /// </summary> /// <param name="writer">Destination to write XML to.</param> public void WriteXml(XmlWriter writer) { if (writer is null) { throw new ArgumentNullException(nameof(writer)); } writer.WriteAttributeString("Id", Id); if (Headcode != null) { writer.WriteElementString("Headcode", Headcode); } if (LocoDiagram != null) { writer.WriteElementString("LocoDiagram", LocoDiagram); } if (TrainClassId != null) { writer.WriteElementString("TrainClassId", TrainClassId); } if (GraphProperties != null) { writer.WriteStartElement("GraphProperties"); GraphProperties.WriteXml(writer); writer.WriteEndElement(); } writer.WriteStartElement("TrainTimes"); foreach (TrainLocationTimeModel time in TrainTimes) { writer.WriteStartElement("Time"); time.WriteXml(writer); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("FootnoteIds"); foreach (string note in FootnoteIds) { writer.WriteElementString("Note", note); } writer.WriteEndElement(); writer.WriteElementString("IncludeSeparatorAbove", IncludeSeparatorAbove ? "true" : "false"); writer.WriteElementString("IncludeSeparatorBelow", IncludeSeparatorBelow ? "true" : "false"); writer.WriteElementString("InlineNote", InlineNote); if (ToWork != null) { writer.WriteStartElement("ToWork"); ToWork.WriteXml(writer); writer.WriteEndElement(); } if (LocoToWork != null) { writer.WriteStartElement("LocoToWork"); LocoToWork.WriteXml(writer); writer.WriteEndElement(); } }
/// <summary> /// Create a partially deep copy of this train. The <see cref="TrainTimes"/> property is deep-copied; other properties are shallow-copied. /// </summary> /// <returns>A copy of the current instance.</returns> public Train Copy() { Train t = new Train { Headcode = Headcode, LocoDiagram = LocoDiagram, TrainClass = TrainClass, TrainClassId = TrainClassId, IncludeSeparatorAbove = IncludeSeparatorAbove, IncludeSeparatorBelow = IncludeSeparatorBelow, InlineNote = InlineNote, GraphProperties = GraphProperties.Copy(), ToWork = ToWork?.Copy(), LocoToWork = LocoToWork?.Copy(), }; foreach (TrainLocationTime tlt in TrainTimes) { t.TrainTimes.Add(tlt.Copy()); } foreach (Note fn in Footnotes as IEnumerable <Note> ) { t.Footnotes.Add(fn); } return(t); }
//https://leetcode.com/problems/is-graph-bipartite/ /* * Given an undirected graph, return true if and only if it is bipartite. * * Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B. * * The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn't contain any element twice. */ public bool IsBipartite(int[][] adjacencyMatrix) { var nodes = new HashSet <GraphNode <int> >(); var edges = new List <UndirectedEdge <int> >(); for (int i = 0; i < adjacencyMatrix.GetLength(0); i++) { for (int j = 0; j < adjacencyMatrix[i].Length; j++) { var node_i = new GraphNode <int>(i); node_i = nodes.TryGetValue(node_i, out var node) ? node : node_i; nodes.Add(node_i); var node_j = new GraphNode <int>(adjacencyMatrix[i][j]); node_j = nodes.TryGetValue(node_j, out node) ? node : node_j; nodes.Add(node_j); edges.Add(new UndirectedEdge <int>(node_i, node_j)); } } var graph = new UndirectedGraph <int>(nodes, edges); return(GraphProperties <int> .IsBipartite(graph)); }
public void IsConnected(TestData testGraph) { var(isConnected, subgraphCount) = GraphProperties.IsConnected(testGraph.Graph); Assert.Equal(testGraph.IsConnected, isConnected); Assert.Equal(testGraph.SubgraphCount, subgraphCount); }
public void TestCase_0() { var edges = new List <DirectedEdge <int> >(); var graph = new DirectedGraph <int>(edges); Assert.True(GraphProperties <int> .IsDirectedAcyclic(graph)); }
public void TestCase_1() { var edges = new List <DirectedEdge <int> >() { new DirectedEdge <int>(new GraphNode <int>(1), new GraphNode <int>(2)) }; var graph = new DirectedGraph <int>(edges); Assert.True(GraphProperties <int> .IsDirectedAcyclic(graph)); }
public void TestCase_2() { var node1 = new GraphNode <int>(1); var node2 = new GraphNode <int>(2); var edges = new List <DirectedEdge <int> >() { new DirectedEdge <int>(node1, node2), new DirectedEdge <int>(node2, node1) }; var graph = new DirectedGraph <int>(edges); Assert.False(GraphProperties <int> .IsDirectedAcyclic(graph)); }
//https://leetcode.com/problems/course-schedule/ public bool CanFinish(int numCourses, int[][] prerequisites) { var nodes = new List <GraphNode <int> >(numCourses); for (var i = 0; i < numCourses; i++) { nodes.Add(new GraphNode <int>(i)); } var edges = new List <DirectedEdge <int> >(prerequisites.GetLength(0)); for (var i = 0; i < prerequisites.GetLength(0); i++) { edges.Add(new DirectedEdge <int>(nodes[prerequisites[i][0]], nodes[prerequisites[i][1]])); } var diGraph = new DirectedGraph <int>(edges); return(GraphProperties <int> .IsDirectedAcyclic(diGraph)); }
public void DirectedGraphConnectedness() { var nodes = new List <GraphNode <int> >() { new GraphNode <int>(0), new GraphNode <int>(1), new GraphNode <int>(2) }; var edges = new List <DirectedEdge <int> >() { new DirectedEdge <int>(nodes[1], nodes[2]) }; var graph = new DirectedGraph <int>(nodes, edges); Assert.False(GraphProperties <int> .InSameConnectedComponent(graph, new List <GraphNode <int> >() { nodes[0], nodes[1], nodes[2] })); }
private async void CreateButton_ClickAsync(object sender, RoutedEventArgs e) { //Disable Control to prevent this method from running parallely multiple times CreateButton.IsEnabled = false; //Reset Edges Label ShowGraphEdgesLabel.Content = ""; //Create an array of edges Edge[] edges = await CreateEdgesArrayAsync(GraphEdgesTextBox.Text); if (edges.Length > 0) { //Create a graph by using an array of edges. No extra vertices need to be declared. graph = new Graph(new Vertex[] { }, edges); //Update first label according to the Completeness of the created graph UpdateIsGraphCompleteLabel(await GraphProperties.IsGraphCompleteAsync(graph)); //Update second label according to the Bipartition of the created graph UpdateIsGraphBipartiteLabel(await GraphProperties.IsGraphBipartiteAsync(graph)); //Update third label according to the Connectivity of the created graph UpdateIsGraphConnectedLabel(await GraphProperties.IsGraphConnectedAsync(graph)); //Reset Dijkstra Path Label DijkstraPathLabel.Content = ""; //Update Edges Label ShowGraphEdgesLabel.Content = "Current Graph: " + GraphEdgesTextBox.Text; } else { GraphEdgesTextBox.Text = ""; GraphEdgesSampleLabel.Content = "Please use this pattern: (A,B)(A,C)(B,C)"; } //Enable Controls again CreateButton.IsEnabled = true; }
/// <summary> /// Serialize this object to XML. /// </summary> /// <param name="writer">The destination to write the serialized object data to.</param> public void WriteXml(XmlWriter writer) { if (writer is null) { throw new ArgumentNullException(nameof(writer)); } writer.WriteAttributeString("Id", Id); if (Headcode != null) { writer.WriteElementString("Headcode", Headcode); } if (TrainClassId != null) { writer.WriteElementString("TrainClassId", TrainClassId); } if (GraphProperties != null) { writer.WriteStartElement("GraphProperties"); GraphProperties.WriteXml(writer); writer.WriteEndElement(); } writer.WriteStartElement("TrainTimes"); foreach (TrainLocationTimeModel time in TrainTimes) { writer.WriteStartElement("Time"); time.WriteXml(writer); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("FootnoteIds"); foreach (string note in FootnoteIds) { writer.WriteElementString("Note", note); } writer.WriteEndElement(); }
public void DirectedGraph_oneComponent() { var nodeList = new List <GraphNode <int> >() { new GraphNode <int>(0), new GraphNode <int>(1), new GraphNode <int>(2) }; var edges = new List <DirectedEdge <int> >() { new DirectedEdge <int>(nodeList[0], nodeList[1]), new DirectedEdge <int>(nodeList[1], nodeList[2]), new DirectedEdge <int>(nodeList[2], nodeList[0]) }; var graph = new DirectedGraph <int>(nodeList, edges); Assert.True(GraphProperties <int> .InSameConnectedComponent(graph, new List <GraphNode <int> >() { nodeList[1], nodeList[2] })); }
public static InvalidGraphPropertiesException MustHave(GraphProperties property) { return(new InvalidGraphPropertiesException("The property " + property.ToString() + " is not set but must exist for this algorithm.")); }
public GraphPropertiesViewModel(IGraph graph) { Properties = new GraphProperties(graph); Properties.EvaluteAll(); }
public void IsTree(TestData testGraph) => Assert.Equal( testGraph.IsTree, GraphProperties.IsTree( GraphProperties.IsConnected(testGraph.Graph).isConnected, GraphProperties.IsCyclic(testGraph.Graph)));
public void IsCyclic(TestData testGraph) => Assert.Equal(testGraph.IsCyclic, GraphProperties.IsCyclic(testGraph.Graph));
public void IsBipartite(TestData testGraph) => Assert.Equal(testGraph.IsBipartite, GraphProperties.IsBipartite(testGraph.Graph));