private void Debug() { CIt_GraphNodes it = new CIt_GraphNodes(mGraph); for (it.Begin(); !it.End(); it.Next()) { Console.WriteLine("{0}.distance={1}", it.M_CurrentItem.M_Label, Distance(it.M_CurrentItem)); if (Predecessor(it.M_CurrentItem) != null) { Console.WriteLine("{0}.predecessor={1}", it.M_CurrentItem.M_Label, Predecessor(it.M_CurrentItem).M_Label); } } Console.WriteLine("Shortest Paths {0}->*", m_source.M_Label); for (it.Begin(); !it.End(); it.Next()) { foreach (var cGraphNode in m_shortestPaths[m_source][it.M_CurrentItem]) { Console.Write("{0},", cGraphNode.M_Label); } Console.WriteLine(); } }
public override void Init() { CIt_GraphNodes itn = new CIt_GraphNodes(mGraph); m_shortestPaths[m_source] = new Dictionary <CGraphNode, Path>(); for (itn.Begin(); !itn.End(); itn.Next()) { if (itn.M_CurrentItem != m_source) { m_outputShortestPathsInfo.CreateInfo(itn.M_CurrentItem, new NodePathInfo() { MDistance = null, MPredecessor = null }); } else { m_outputShortestPathsInfo.CreateInfo(itn.M_CurrentItem, new NodePathInfo() { MDistance = 0, MPredecessor = null }); } } }
public override StringBuilder Print() { StringBuilder FAText = new StringBuilder(1000); // Output Nodes CIt_GraphNodes itn = new CIt_GraphNodes(m_graph); for (itn.Begin(); !itn.End(); itn.Next()) { FAText.Append(itn.M_CurrentItem.M_Label); if (!itn.M_LastItem) { FAText.Append(", "); } else { FAText.Append("\n\n"); } } // Output edges CIt_GraphEdges ite = new CIt_GraphEdges(m_graph); for (ite.Begin(); !ite.End(); ite.Next()) { FAText.Append("\n" + ite.M_CurrentItem.M_Source.M_Label + "---" + m_FAInfo.Info(ite.M_CurrentItem).M_TransitionCharSet.ToString() + "---->" + ite.M_CurrentItem.M_Target.M_Label); } return(FAText); }
public override FA VisitRegexpAlternation(CASTElement currentNode) { CRegexpAlternation altNode = currentNode as CRegexpAlternation; CSubsetConstructionAlgorithm subcon; CHopcroftAlgorithm hopmin; //1. Create FA CThompsonAlternationTemplate alttempSyn = new CThompsonAlternationTemplate(this.GetHashCode()); FA leftFa = Visit(altNode.GetChild(ContextType.CT_REGEXPALTERNATION_TERMS, 0)); CIt_GraphNodes it = new CIt_GraphNodes(leftFa); for (it.Begin(); !it.End(); it.Next()) { leftFa.PrefixElementLabel(leftFa.GetFANodePrefix(it.M_CurrentItem), it.M_CurrentItem); } FA rightFa = Visit(altNode.GetChild(ContextType.CT_REGEXPALTERNATION_TERMS, 1)); it = new CIt_GraphNodes(rightFa); for (it.Begin(); !it.End(); it.Next()) { rightFa.PrefixElementLabel(rightFa.GetFANodePrefix(it.M_CurrentItem), it.M_CurrentItem); } //2.Synthesize the two FAs to a new one m_currentNFA = alttempSyn.Sythesize(leftFa, rightFa, CGraph.CMergeGraphOperation.MergeOptions.MO_DEFAULT); m_ReportingServices.ExctractThompsonStep(m_currentNFA, @"Alternation_" + m_currentNFA.M_Label + ".dot", this.GetHashCode()); m_ReportingServices.AddThompsonStepToReporting(m_currentNFA, this.GetHashCode()); //return the final-synthesized FA return(m_currentNFA); }
public override void Init() { CIt_GraphNodes it = new CIt_GraphNodes(m_graph); m_BFSData.CreateInfo(m_nodeVisitList); for (it.Begin(); !it.End(); it.Next()) { if (it.M_CurrentItem != m_source) { m_BFSData.CreateInfo(it.M_CurrentItem, new BFSNodeInfo() { MDistance = -1, MColor = NodeColor.NC_WHITE }); } else { m_BFSData.CreateInfo(it.M_CurrentItem, new BFSNodeInfo() { MDistance = 0, MColor = NodeColor.NC_GRAY }); } } m_Q.Enqueue(m_source); }
public static void TestCaseBook() { // 1. Create a graph CGraph mgraph = CGraph.CreateGraph(); CGraphNode u = mgraph.CreateGraphNode <CGraphNode>("u"); CGraphNode v = mgraph.CreateGraphNode <CGraphNode>("v"); CGraphNode w = mgraph.CreateGraphNode <CGraphNode>("w"); CGraphNode x = mgraph.CreateGraphNode <CGraphNode>("x"); CGraphNode y = mgraph.CreateGraphNode <CGraphNode>("y"); CGraphNode z = mgraph.CreateGraphNode <CGraphNode>("z"); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(u, v, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(u, x, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x, v, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(v, y, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(y, x, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(w, y, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(w, z, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(z, z, GraphType.GT_DIRECTED); DepthFirstSearch dfs = new DepthFirstSearch(mgraph); dfs.Run(); DepthFirstSearchQueryInfo info = new DepthFirstSearchQueryInfo(mgraph, dfs); CIt_GraphNodes it = new CIt_GraphNodes(mgraph); for (it.Begin(); !it.End(); it.Next()) { Console.WriteLine("Node {0}: arrival ({1}) - departure ({2})", it.M_CurrentItem.M_Label, info.Arrival(it.M_CurrentItem), info.Departure(it.M_CurrentItem)); } }
private FA CreateNewFA(FA synth) { CGraphNode oldEntryNode, oldExitNode; FA m_currentFA = new FA(); m_ThompsonInfo = new ThompsonInfo(m_currentFA, m_ThompsonInfoKey); m_ThompsonInfo.InitFAInfo(new ThompsonFAInfo()); //2.Merge graph m_mergeOperation = m_currentFA.Merge(synth, CGraph.CMergeGraphOperation.MergeOptions.MO_DEFAULT); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_NODE, FA.m_FAINFOKEY); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_EDGE, m_ThompsonInfoKey); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_GRAPH, m_ThompsonInfoKey); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_NODE, m_ThompsonInfoKey); // Create boundary nodes m_newFASource = m_currentFA.CreateGraphNode <CGraphNode>(); m_ThompsonInfo.InitNodeInfo(m_newFASource, new ThompsonNodeFAInfo()); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(m_newFASource, m_mergeOperation.GetMirrorNode(synth.M_Initial), GraphType.GT_DIRECTED); m_newFATarget = m_currentFA.CreateGraphNode <CGraphNode>(); m_ThompsonInfo.InitNodeInfo(m_newFATarget, new ThompsonNodeFAInfo()); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(m_mergeOperation.GetMirrorNode(synth.GetFinalStates()[0]), m_newFATarget, GraphType.GT_DIRECTED); //4.Create the initial and the final node oldEntryNode = m_mergeOperation.GetMirrorNode(synth.M_Initial); oldExitNode = m_mergeOperation.GetMirrorNode(synth.GetFinalStates()[0]); m_currentFA.M_Initial = m_newFASource; m_currentFA.SetFinalState(m_newFATarget); m_currentFA.UpdateAlphabet(); // Update closure information from operand closure FAInfo currentFAInfo = UpdateClosureInformation(synth, m_currentFA, m_mergeOperation); // Update closure information from current closure m_currentFAloop = new FALoop(); m_currentFAloop.MEntryNode = oldEntryNode; m_currentFAloop.MExitNode = oldExitNode; m_currentFAloop.MLoopSerial = m_currentClosureSerial; CIt_GraphNodes it = new CIt_GraphNodes(m_currentFA); for (it.Begin(); !it.End(); it.Next()) { if (it.M_CurrentItem != m_currentFA.M_Initial && it.M_CurrentItem != m_currentFA.GetFinalStates()[0]) { m_currentFAloop.MParticipatingNodes.Add(it.M_CurrentItem); } } // Add new closure to the current FA currentFAInfo.AddFALoop(m_currentFAloop); return(m_currentFA); }
public override FA VisitRegexpClosure(CASTElement currentNode) { CRegexpClosure closNode = currentNode as CRegexpClosure; //1.Create FA CThompsonClosureTemplate newFA = new CThompsonClosureTemplate(this.GetHashCode(), currentNode.M_Text); //2.Check the type of the closure if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_NONEORMULTIPLE) { FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesizeNoneOrMul(customFA); } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_ONEORMULTIPLE) { FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesisOneOrMul(customFA); } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_ONEORZERO) { FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesizeOneOrNone(customFA); } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_FINITECLOSURE) { CClosureRange rangeNode = closNode.GetChild(ContextType.CT_REGEXPCLOSURE_QUANTIFIER, 0) as CClosureRange; FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesizeFinite(customFA, rangeNode.M_ClosureMultiplicityLB, rangeNode.M_ClosureMultiplicityUB); } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_NONEORMULTIPLE_NONGREEDY) { //TODO } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_ONEORMULTIPLE_NONGREEDY) { //TODO } else { Console.WriteLine("No proper input"); } // Prefix the nodes of the new NFA with the prefix for the current regular expression CIt_GraphNodes it = new CIt_GraphNodes(m_currentNFA); for (it.Begin(); !it.End(); it.Next()) { m_currentNFA.PrefixElementLabel(m_currentRegularExpression.M_StatementID, it.M_CurrentItem); } m_ReportingServices.ExctractThompsonStep(m_currentNFA, @"Closure_" + m_currentNFA.M_Label + ".dot", this.GetHashCode()); m_ReportingServices.AddThompsonStepToReporting(m_currentNFA, this.GetHashCode()); //4.Pass FA to the predecessor return(m_currentNFA); }
public override FA VisitLexerDescription(CASTElement currentNode) { int i = 0; FA leftFa = null, rightFa; CGraph.CMergeGraphOperation.MergeOptions mergeOptions = CGraph.CMergeGraphOperation.MergeOptions.MO_DEFAULT; CSubsetConstructionAlgorithm subcon; CHopcroftAlgorithm hopmin; CLexerDescription lexerDescription = currentNode as CLexerDescription; List <CASTElement> rExpStatements = lexerDescription.GetContextChildren(ContextType.CT_LEXERDESCRIPTION_BODY); // Preserve labels of RegExp-derived NFA if (!m_options.IsSet(ThompsonOptions.TO_STEPS)) { mergeOptions = CGraph.CMergeGraphOperation.MergeOptions.MO_PRESERVELABELS; } //1. Create FA foreach (var rExpStatement in rExpStatements) { if (i > 0) { rightFa = Visit(rExpStatement); //2.Synthesize the two FAs to a new one CThompsonAlternationTemplate alttempSyn = new CThompsonAlternationTemplate(this.GetHashCode()); leftFa = alttempSyn.Sythesize(leftFa, rightFa, mergeOptions); // Prefix node elements of the resulting graph with CIt_GraphNodes it = new CIt_GraphNodes(leftFa); for (it.Begin(); !it.End(); it.Next()) { leftFa.PrefixElementLabel(leftFa.GetFANodePrefix(it.M_CurrentItem), it.M_CurrentItem); } } else { leftFa = Visit(rExpStatement); } i++; } m_NFA = leftFa; m_NFA.UpdateAlphabet(); m_ReportingServices.ExctractThompsonStep(m_NFA, @"merge.dot", this.GetHashCode()); if (i > 1) { m_ReportingServices.AddThompsonStepToReporting(m_NFA, this.GetHashCode(), true); } else { m_ReportingServices.ThompsonStepsGenerate(); } //return the final-synthesized FA return(m_NFA); }
public void FindAllPairsShortestPaths() { CIt_GraphNodes itn = new CIt_GraphNodes(mGraph); for (itn.Begin(); !itn.End(); itn.Next()) { m_source = itn.M_CurrentItem; Run(); } }
public void Debug() { DepthFirstSearchQueryInfo info = new DepthFirstSearchQueryInfo(m_graph, this); CIt_GraphNodes it = new CIt_GraphNodes(m_graph); for (it.Begin(); !it.End(); it.Next()) { Console.WriteLine("Node {0}: arrival ({1}) - departure ({2})", it.M_CurrentItem.M_Label, info.Arrival(it.M_CurrentItem), info.Departure(it.M_CurrentItem)); } }
/// <summary> /// Gives the initial mapping of labels to elements. /// Called by the constructor. Must be defined in subclasses /// </summary> protected override void LabelElements() { string label; // Create iterator CIt_GraphNodes it = new CIt_GraphNodes(m_graph); for (it.Begin(); !it.End(); it.Next()) { label = it.M_CurrentItem.ToString(); SetLabel(it.M_CurrentItem, label); } }
public override void Init() { CIt_GraphNodes it = new CIt_GraphNodes(m_graph); for (it.Begin(); !it.End(); it.Next()) { m_outputDepthFirstSearch.CreateInfo(it.M_CurrentItem, new DepthFirstSearchNodeInfo() { MColor = NodeColor.NC_WHITE, MDeparture = -1, MArrival = -1 }); } m_time = 0; for (it.Begin(); !it.End(); it.Next()) { if (Color(it.M_CurrentItem) == NodeColor.NC_WHITE) { Visit(it.M_CurrentItem); } } }
protected override void LabelElements() { string label; int serialNumber = 0; // Create iterator CIt_GraphNodes it = new CIt_GraphNodes(m_graph); for (it.Begin(); !it.End(); it.Next()) { label = m_prefix + serialNumber++; SetLabel(it.M_CurrentItem, label); } }
private void GeneratePaths() { CIt_GraphNodes it = new CIt_GraphNodes(mGraph); CGraphNode m_node; for (it.Begin(); !it.End(); it.Next()) { m_shortestPaths[m_source][it.M_CurrentItem] = new Path(); m_node = it.M_CurrentItem; while (m_node != null) { m_shortestPaths[m_source][it.M_CurrentItem].Insert(m_node, 0); m_node = Predecessor(m_node); } } }
/// <summary> /// Returns a string that contains the element and its info contents /// </summary> /// <returns></returns> public override string ToString() { StringBuilder sBuilder = new StringBuilder(); string s = ""; switch (M_ElementType) { case GraphElementType.ET_NODE: s = "Node "; break; case GraphElementType.ET_EDGE: s = "Edge "; break; case GraphElementType.ET_GRAPH: s = "Graph "; break; } sBuilder.Append(s + M_Label + ":"); sBuilder.AppendLine(); foreach (KeyValuePair <object, object> key in m_algorithmOutput) { sBuilder.Append("key :" + key.Key.ToString()); sBuilder.Append("info :" + key.Value.ToString()); } if (M_ElementType == GraphElementType.ET_GRAPH) { CIt_GraphNodes itn = new CIt_GraphNodes(this as CGraph); for (itn.Begin(); !itn.End(); itn.Next()) { sBuilder.Append(itn.M_CurrentItem.ToString()); sBuilder.AppendLine(); } CIt_GraphEdges itg = new CIt_GraphEdges(this as CGraph); for (itg.Begin(); !itg.End(); itg.Next()) { sBuilder.Append(itg.M_CurrentItem.ToString()); sBuilder.AppendLine(); } } return(sBuilder.ToString()); }
public override FA VisitRegexpClosure(CASTElement currentNode) { CRegexpClosure closNode = currentNode as CRegexpClosure; //1.Create FA CThompsonClosureTemplate newFA = new CThompsonClosureTemplate(); //2.Check the type of the closure if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_NONEORMULTIPLE) { FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesizeNoneOrMul(customFA); } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_ONEORMULTIPLE) { FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesisOneOrMul(customFA); } else if (closNode.M_ClosureType == CRegexpClosure.ClosureType.CLT_ONEORZERO) { FA customFA = Visit(closNode.GetChild(ContextType.CT_REGEXPCLOSURE_REGEXP, 0)); m_currentNFA = newFA.SynthesizeOneOrNone(customFA); } else { Console.WriteLine("No proper input"); } CIt_GraphNodes it = new CIt_GraphNodes(m_currentNFA); for (it.Begin(); !it.End(); it.Next()) { m_currentNFA.PrefixElementLabel(m_currentRegularExpression.M_StatementID, it.M_CurrentItem); } m_ReportingServices.ExctractThompsonStep(m_currentNFA, @"../bin/Debug/Closure_" + m_currentNFA.M_Label + ".dot"); m_ReportingServices.AddThompsonStepToReporting(m_currentNFA); //4.Pass FA to the predecessor return(m_currentNFA); }
public override void Init() { CGraphNode newSTNode; m_dfsSpanningTree = CDFSSpanningTree.CreateGraph(); CIt_GraphNodes it = new CIt_GraphNodes(m_sourceGraphs[0]); CIt_GraphRootNodes it1 = new CIt_GraphRootNodes(m_sourceGraphs[0]); // Paint the nodes white color (0) for (it.Begin(); !it.End(); it.Next()) { //CreateInfo(it.M_CurrentItem, // new NodeInfo_DFS(){ m_color = 0}, // this); newSTNode = m_dfsSpanningTree.CreateGraphNode("L" + it.M_CurrentItem.M_Label); newSTNode[m_dfsSpanningTree] = new NodeInfo_DFSSpanningTree() { M_Preorder = 0, M_Postorder = 0 }; m_Mappping.SetOriginalToDerivedNode(it.M_CurrentItem, newSTNode); } // Start at a random node. If the graph is not completely traversed // the Visit function will return and another node ( not already // visited ) will be used as starting node. This works fine for // undirected graphs. For tree graphs it is recomended to start // from the tree root node because a different visiting pattern is // produced for different starting nodes for (it1.Begin(); !it1.End(); it1.Next()) { if (Color(it1.M_CurrentItem) == 0) { Visit(it1.M_CurrentItem); } } }
public override FA VisitRegexpConcatenation(CASTElement currentNode) { CRegexpConcatenation altNode = currentNode as CRegexpConcatenation; //1. Create FA CThompsonConcatenationTemplate alttempSyn = new CThompsonConcatenationTemplate(this.GetHashCode()); FA leftFa = Visit(altNode.GetChild(ContextType.CT_REGEXPCONCATENATION_TERMS, 0)); FA rightFa = Visit(altNode.GetChild(ContextType.CT_REGEXPCONCATENATION_TERMS, 1)); //2.Synthesize the two FAs to a new one m_currentNFA = alttempSyn.Synthesize(leftFa, rightFa); CIt_GraphNodes it = new CIt_GraphNodes(m_currentNFA); for (it.Begin(); !it.End(); it.Next()) { m_currentNFA.PrefixElementLabel(m_currentRegularExpression.M_StatementID, it.M_CurrentItem); } m_ReportingServices.ExctractThompsonStep(m_currentNFA, @"Concatenation_" + m_currentNFA.M_Label + ".dot", this.GetHashCode()); m_ReportingServices.AddThompsonStepToReporting(m_currentNFA, this.GetHashCode()); return(m_currentNFA); }
public override StringBuilder Print() { StringBuilder graphvizStringBuilder = new StringBuilder(1000); string header = "digraph G" + m_graph.M_SerialNumber + "{\r\n"; string graphedge_operator = "->"; CIt_GraphNodes itn = new CIt_GraphNodes(m_graph); CIt_GraphEdges itg = new CIt_GraphEdges(m_graph); FA fa = m_graph as FA; // Print header if necessary // Print the header if the graph is printed alone independently and not // in the context for example of a multigraph printing if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS)) { graphvizStringBuilder.Append(header); } for (itn.Begin(); !itn.End(); itn.Next()) { if (fa.GetFinalStates().Count != 0 && fa.GetFinalStates().Contains(itn.M_CurrentItem)) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [peripheries=2];\n"); } else if (itn.M_CurrentItem == fa.M_Initial) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=green];\n"); } else if (m_thompsonInfo.IsNodeClosureEntrance(itn.M_CurrentItem)) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=red];\n"); } else if (m_thompsonInfo.IsNodeClosureExit(itn.M_CurrentItem)) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=purple];\n"); } else { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\";\n"); } } // Print all edges of the graph for (itg.Begin(); !itg.End(); itg.Next()) { CGraphEdge g = itg.M_CurrentItem; string source, target; source = g.M_Source.M_Label; target = g.M_Target.M_Label; graphvizStringBuilder.AppendFormat("\"{0}\"" + graphedge_operator + "\"{1}\"", source, target); string s = m_FAInfo.Info(g).M_TransitionCharSet?.ToString(); if (s != null) { graphvizStringBuilder.AppendFormat(" [style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]"); } else { if (m_thompsonInfo.IsNodeClosureExit(g.M_Source) && m_thompsonInfo.IsNodeClosureEntrance(g.M_Target)) { graphvizStringBuilder.AppendFormat(" [color=red,style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]"); } } graphvizStringBuilder.Append(";\r\n"); } // Print footer if necessary // Print the header if the graph is printed alone independently and not // in the context for example of a multigraph printing if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS)) { graphvizStringBuilder.Append("}\r\n"); } return(graphvizStringBuilder); }
public override StringBuilder Print() { // Allocate a stringbuiler object and allocate space for 1000 characters StringBuilder graphvizStringBuilder = new StringBuilder(1000); CIt_GraphNodes itn = new CIt_GraphNodes(m_graph); CIt_GraphEdges itg = new CIt_GraphEdges(m_graph); CSubsetConstructionInfo subsetInfo = new CSubsetConstructionInfo(m_graph, m_subsetInfoKey); Dictionary <int, List <CGraphNode> > closuresMap; FA fa = m_graph as FA; string graphedge_operator = "->"; closuresMap = subsetInfo.GetClosureNodesMapping(); //1. generate header string header = "digraph G" + m_graph.M_SerialNumber + "{\r\n"; graphvizStringBuilder.Append(header); foreach (KeyValuePair <int, List <CGraphNode> > closure in closuresMap) { string subgraphHeader = "\tsubgraph cluster" + closure.Key + " {\r\n"; string subgraphBody = "\t\tnode [style=filled];\r\n" + "\t\tstyle=filled;\r\n" + "\t\tcolor=lightgrey;\r\n" + "\t\tlabel =\"" + subsetInfo.Info(closure.Value[0]).M_ClosureExpression + "\";\r\n\t\t"; graphvizStringBuilder.Append(subgraphHeader + subgraphBody); foreach (CGraphNode node in closure.Value) { graphvizStringBuilder.Append(node.M_Label + ";"); } graphvizStringBuilder.AppendLine(); graphvizStringBuilder.Append("\t}"); } // Print all nodes for (itn.Begin(); !itn.End(); itn.Next()) { if (fa.GetFinalStates().Count != 0 && fa.GetFinalStates().Contains(itn.M_CurrentItem)) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [peripheries=2];\n"); } else if (itn.M_CurrentItem == fa.M_Initial) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=green];\n"); } else { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\";\n"); } } // Print all edges of the graph for (itg.Begin(); !itg.End(); itg.Next()) { CGraphEdge g = itg.M_CurrentItem; string source, target; source = g.M_Source.M_Label; target = g.M_Target.M_Label; graphvizStringBuilder.AppendFormat("\"{0}\"" + graphedge_operator + "\"{1}\"", source, target); string s = m_FAInfo.Info(g).M_TransitionCharSet?.ToString(); if (s != null) { graphvizStringBuilder.AppendFormat(" [style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]"); } else { } graphvizStringBuilder.Append(";\r\n"); } graphvizStringBuilder.Append("}\r\n"); return(graphvizStringBuilder); }
public static void BookExampleTestcase() { // 1. Create a graph CGraph mgraph = CGraph.CreateGraph(); CGraphNode x = mgraph.CreateGraphNode <CGraphNode>("x"); CGraphNode y = mgraph.CreateGraphNode <CGraphNode>("y"); CGraphNode z = mgraph.CreateGraphNode <CGraphNode>("z"); CGraphNode t = mgraph.CreateGraphNode <CGraphNode>("t"); CGraphNode s = mgraph.CreateGraphNode <CGraphNode>("s"); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(t, x, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(t, y, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(t, z, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x, t, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(y, x, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(y, z, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(z, x, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(z, s, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(s, t, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(s, y, GraphType.GT_DIRECTED); // 2. Associate weights with the edges of the graph CGraphQueryInfo <int, int, int> weights = new CGraphQueryInfo <int, int, int>(mgraph, 255); weights.CreateInfo(y, x, -3); weights.CreateInfo(x, t, -2); weights.CreateInfo(t, x, 5); weights.CreateInfo(z, x, 7); weights.CreateInfo(y, z, 9); weights.CreateInfo(t, y, 8); weights.CreateInfo(t, z, -4); weights.CreateInfo(s, t, 6); weights.CreateInfo(s, y, 7); weights.CreateInfo(z, s, 2); // 3. Run the BellmanFord algorithm BellmanFord bl = new BellmanFord(mgraph, s, 255); bl.FindAllPairsShortestPaths(); // 4. Print Paths CGraphQueryInfo <int, int, Dictionary <CGraphNode, Dictionary <CGraphNode, Path> > > shortestPath = new CGraphQueryInfo <int, int, Dictionary <CGraphNode, Dictionary <CGraphNode, Path> > >(mgraph, bl); CIt_GraphNodes i = new CIt_GraphNodes(mgraph); CIt_GraphNodes j = new CIt_GraphNodes(mgraph); Dictionary <CGraphNode, Dictionary <CGraphNode, Path> > paths = (Dictionary <CGraphNode, Dictionary <CGraphNode, Path> >)(shortestPath.Info()); for (i.Begin(); !i.End(); i.Next()) { Console.WriteLine(); for (j.Begin(); !j.End(); j.Next()) { Console.WriteLine(); if (i.M_CurrentItem != j.M_CurrentItem) { Console.Write(paths[i.M_CurrentItem][j.M_CurrentItem]); } } } }
public static void TestCase3x3() { // 1. Create a graph CGraph mgraph = CGraph.CreateGraph(); CGraphNode x1 = mgraph.CreateGraphNode <CGraphNode>("1"); CGraphNode x2 = mgraph.CreateGraphNode <CGraphNode>("2"); CGraphNode x3 = mgraph.CreateGraphNode <CGraphNode>("3"); CGraphNode x4 = mgraph.CreateGraphNode <CGraphNode>("4"); CGraphNode x5 = mgraph.CreateGraphNode <CGraphNode>("5"); CGraphNode x6 = mgraph.CreateGraphNode <CGraphNode>("6"); CGraphNode x7 = mgraph.CreateGraphNode <CGraphNode>("7"); CGraphNode x8 = mgraph.CreateGraphNode <CGraphNode>("8"); CGraphNode x9 = mgraph.CreateGraphNode <CGraphNode>("9"); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x1, x2, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x2, x1, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x1, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x1, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x1, x4, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x4, x1, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x2, x4, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x4, x2, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x2, x3, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x3, x2, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x2, x6, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x6, x2, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x2, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x2, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x3, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x3, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x3, x6, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x6, x3, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x4, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x4, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x4, x7, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x7, x4, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x4, x8, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x8, x4, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x6, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x6, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x7, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x7, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x8, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x8, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x5, x9, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x9, x5, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x6, x8, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x8, x6, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x6, x9, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x9, x6, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x7, x8, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x8, x7, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x8, x9, GraphType.GT_DIRECTED); mgraph.AddGraphEdge <CGraphEdge, CGraphNode>(x9, x8, GraphType.GT_DIRECTED); // 2. Associate weights with the edges of the graph CGraphQueryInfo <int, int, int> weights = new CGraphQueryInfo <int, int, int>(mgraph, 255); weights.CreateInfo(x1, x2, 1); weights.CreateInfo(x2, x1, 1); weights.CreateInfo(x1, x5, 1); weights.CreateInfo(x5, x1, 1); weights.CreateInfo(x1, x4, 1); weights.CreateInfo(x4, x1, 1); weights.CreateInfo(x2, x4, 3); weights.CreateInfo(x4, x2, 3); weights.CreateInfo(x2, x3, 1); weights.CreateInfo(x3, x2, 1); weights.CreateInfo(x2, x6, 3); weights.CreateInfo(x6, x2, 3); weights.CreateInfo(x2, x5, 1); weights.CreateInfo(x5, x2, 1); weights.CreateInfo(x3, x5, 3); weights.CreateInfo(x5, x3, 3); weights.CreateInfo(x3, x6, 1); weights.CreateInfo(x6, x3, 1); weights.CreateInfo(x4, x5, 1); weights.CreateInfo(x5, x4, 1); weights.CreateInfo(x4, x7, 1); weights.CreateInfo(x7, x4, 1); weights.CreateInfo(x4, x8, 3); weights.CreateInfo(x8, x4, 3); weights.CreateInfo(x5, x6, 1); weights.CreateInfo(x6, x5, 1); weights.CreateInfo(x5, x7, 3); weights.CreateInfo(x7, x5, 3); weights.CreateInfo(x5, x8, 1); weights.CreateInfo(x8, x5, 1); weights.CreateInfo(x5, x9, 3); weights.CreateInfo(x9, x5, 3); weights.CreateInfo(x6, x8, 3); weights.CreateInfo(x8, x6, 3); weights.CreateInfo(x6, x9, 1); weights.CreateInfo(x9, x6, 1); weights.CreateInfo(x7, x8, 1); weights.CreateInfo(x8, x7, 1); weights.CreateInfo(x8, x9, 1); weights.CreateInfo(x9, x8, 1); // 3. Run the BellmanFord algorithm BellmanFord bl = new BellmanFord(mgraph, x1, 255); bl.FindAllPairsShortestPaths(); // 4. Print Paths BellmanFordQueryInfo shortestPath = new BellmanFordQueryInfo(mgraph, bl); CIt_GraphNodes i = new CIt_GraphNodes(mgraph); CIt_GraphNodes j = new CIt_GraphNodes(mgraph); Dictionary <CGraphNode, Dictionary <CGraphNode, Path> > paths = shortestPath.ShortestPaths(); for (i.Begin(); !i.End(); i.Next()) { Console.WriteLine(); for (j.Begin(); !j.End(); j.Next()) { Console.WriteLine(); if (i.M_CurrentItem != j.M_CurrentItem) { Console.Write(paths[i.M_CurrentItem][j.M_CurrentItem]); } } } }
public void Init() { // List of DFA nodes in minimized DFA node List <CGraphNode> configurationAcc, configurationNonAcc; // Create configuration (min-DFA node) for accepted nodes CGraphNode acceptedConf = m_minimizedDFA.CreateGraphNode <CGraphNode>(); // Create a list of initial-DFA nodes that are registered as accepted nodes configurationAcc = new List <CGraphNode>(); // Store the list for initial-DFA accepted nodes in the min-DFA for accepted nodes SetNodesInConfiguration(acceptedConf, configurationAcc); // Create configuration (min-DFA node) for non-accepted nodes CGraphNode non_acceptedConf = m_minimizedDFA.CreateGraphNode <CGraphNode>(); // Create a list for initial-DFA nodes that are registered as non-accepted nodes configurationNonAcc = new List <CGraphNode>(); // Store the list of initial-DFA non-accepted node in the min-DFA for accepted nodes SetNodesInConfiguration(non_acceptedConf, configurationNonAcc); // Separate accepted from non-accepted nodes into two distinct // configurations. Iterate over the input DFA and place the // accepted node into configurationAcc and none accepted nodes // into configurationNonAcc CIt_GraphNodes it = new CIt_GraphNodes(m_DFA); for (it.Begin(); !it.End(); it.Next()) { if (m_DFA.GetFinalStates().Contains(it.M_CurrentItem)) { // Record in the input DFA node the in which minimized DFA node // will be placed SetNodeConfiguration(it.M_CurrentItem, acceptedConf); configurationAcc.Add(it.M_CurrentItem); } else { // Record in the input DFA node the in which minimized DFA node // will be placed SetNodeConfiguration(it.M_CurrentItem, non_acceptedConf); configurationNonAcc.Add(it.M_CurrentItem); } } // ************************* Debug Initialization *************************** m_DFA.RegisterGraphPrinter(new FATextPrinter(m_DFA)); m_DFA.Generate("HopCroft_InitialDFA_.txt"); m_reporting.SetInitialMinDFAStatesContents(acceptedConf, configurationAcc, non_acceptedConf, configurationNonAcc); int nodeCount = 0; int iteration_count = 0; CIt_GraphNodes minDFA_it = new CIt_GraphNodes(m_minimizedDFA); // Iterate while the algorithm reaches a fixed point state while (nodeCount != m_minimizedDFA.M_NumberOfNodes) { nodeCount = m_minimizedDFA.M_NumberOfNodes; // ************************* Debug *************************** CIterationRecord currentIteration = m_reporting.AddIteration(iteration_count); for (minDFA_it.Begin(); !minDFA_it.End(); minDFA_it.Next()) { // ************************* Debug *************************** currentIteration.M_NodesToInspect.Add(minDFA_it.M_CurrentItem); currentIteration.M_InitialNodesConfiguration.Add(minDFA_it.M_CurrentItem, new List <CGraphNode>(GetNodesInConfiguration(minDFA_it.M_CurrentItem))); Split(minDFA_it.M_CurrentItem); } } // Draw the final edges // Edges between nodes of the initial DFA are mapped to edges between // configurations in minimized-DFA. Thus, edges are drawn between two // min-DFA related configurations when their corresponding nodes are // connected and their transition character set is combined CIt_GraphNodes minit1 = new CIt_GraphNodes(m_minimizedDFA); CIt_GraphNodes minit2 = new CIt_GraphNodes(m_minimizedDFA); List <CGraphNode> confs, conft; CGraphEdge edge, newedge; for (minit1.Begin(); !minit1.End(); minit1.Next()) { for (minit2.Begin(); !minit2.End(); minit2.Next()) { confs = GetNodesInConfiguration(minit1.M_CurrentItem); conft = GetNodesInConfiguration(minit2.M_CurrentItem); foreach (CGraphNode snode in confs) { foreach (CGraphNode tnode in conft) { edge = m_DFA.Edge(snode, tnode); if (edge != null) { // Disallow duplicate edges between nodes of the minimized DFA newedge = m_minimizedDFA.Edge(minit1.M_CurrentItem, minit2.M_CurrentItem); if (newedge == null) { newedge = m_minimizedDFA.AddGraphEdge <CGraphEdge, CGraphNode>(minit1.M_CurrentItem, minit2.M_CurrentItem, GraphType.GT_DIRECTED); } // Add transition characters if (GetMinDFAStateTransitionCharacterSet(newedge) == null) { SetMinDFAStateTransitionCharacterSet(newedge, new CCharRangeSet(false)); m_minimizedDFA.SetFAEdgeInfo(newedge, new CCharRangeSet(false)); } CCharRangeSet charset = GetMinDFAStateTransitionCharacterSet(newedge); m_minimizedDFA.GetFAEdgeInfo(newedge).AddSet(m_DFA.GetFAEdgeInfo(edge)); } } } } } // Detect accepted nodes in minimized DFA CIt_GraphNodes it1 = new CIt_GraphNodes(m_minimizedDFA); for (it1.Begin(); !it1.End(); it1.Next()) { List <CGraphNode> configuration = GetNodesInConfiguration(it1.M_CurrentItem); List <CGraphNode> finals = m_DFA.GetFinalStates(); foreach (CGraphNode node in configuration) { if (finals.Contains(node)) { m_minimizedDFA.SetFinalState(it1.M_CurrentItem); } } } // Detect initial state of minimized DFA for (it1.Begin(); !it1.End(); it1.Next()) { List <CGraphNode> configuration = GetNodesInConfiguration(it1.M_CurrentItem); CGraphNode initial = m_DFA.M_Initial; foreach (CGraphNode node in configuration) { if (initial == node) { m_minimizedDFA.M_Initial = it1.M_CurrentItem; } } } // Set Final minimized-DFA labels for (it1.Begin(); !it1.End(); it1.Next()) { List <CGraphNode> conf = GetNodesInConfiguration(it1.M_CurrentItem); foreach (CGraphNode iNode in conf) { HashSet <string> prefs = m_DFA.GetFANodePrefixLabels(iNode); foreach (string s in prefs) { m_minimizedDFA.SetFANodePrefix(s, it1.M_CurrentItem); } foreach (uint dependency in m_DFA.GetFANodeLineDependencies(iNode)) { m_minimizedDFA.SetFANodeLineDependency(dependency, it1.M_CurrentItem); } } m_minimizedDFA.PrefixElementLabel(m_minimizedDFA.GetFANodePrefix(it1.M_CurrentItem), it1.M_CurrentItem); } m_reporting.Report("HOPCROFTReport.txt"); FASerializer serializer = new FASerializer(m_minimizedDFA); serializer.Print(); m_DFA.RegisterGraphPrinter(new FAGraphVizPrinter(m_minimizedDFA, new UOPCore.Options <ThompsonOptions>())); m_DFA.Generate(@"minimizedDFA.dot", true); }
/// <summary> /// Prints the graph into a StringBuilder object. /// Optionally the header and footer of the .dot file can be ommited for use of the /// graph edges in the multi layer graph printer. /// </summary> /// <param name="onlyedges">if set to <c>true</c> [onlyedges] the graph is printed as a standalone graph.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public override StringBuilder Print() { // Allocate a stringbuiler object and allocate space for 1000 characters StringBuilder graphvizStringBuilder = new StringBuilder(1000); string graphedge_operator = " "; string header = ""; string headerProperties = ""; CIt_GraphNodes itn = new CIt_GraphNodes(m_graph); CIt_GraphEdges itg = new CIt_GraphEdges(m_graph); FA fa = m_graph as FA; switch (m_graph.M_GraphType) { case GraphType.GT_UNDIRECTED: graphedge_operator = "--"; header = "graph G" + m_graph.M_SerialNumber + "{\r\n"; header += headerProperties; break; case GraphType.GT_DIRECTED: graphedge_operator = "->"; header = "digraph G" + m_graph.M_SerialNumber + "{\r\n"; header += headerProperties; break; case GraphType.GT_MIXED: break; } // Print header if necessary // Print the header if the graph is printed alone independently and not // in the context for example of a multigraph printing if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS)) { graphvizStringBuilder.Append(header); } // Print all nodes for (itn.Begin(); !itn.End(); itn.Next()) { if (fa.GetFinalStates().Count != 0 && fa.GetFinalStates().Contains(itn.M_CurrentItem)) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [peripheries=2];\n"); } else if (itn.M_CurrentItem == fa.M_Initial) { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\" [style=filled,fillcolor=green];\n"); } else { graphvizStringBuilder.Append("\"" + itn.M_CurrentItem.M_Label + "\";\n"); } } // Print all edges of the graph for (itg.Begin(); !itg.End(); itg.Next()) { CGraphEdge g = itg.M_CurrentItem; string source, target; source = g.M_Source.M_Label; target = g.M_Target.M_Label; graphvizStringBuilder.AppendFormat("\"{0}\"" + graphedge_operator + "\"{1}\"", source, target); string s = m_FAInfo.Info(g).M_TransitionCharSet?.ToString(); if (s != null) { graphvizStringBuilder.AppendFormat(" [style = bold, label = \"" + m_FAInfo.Info(g).M_TransitionCharSet?.ToString() + "\"]"); } else { } graphvizStringBuilder.Append(";\r\n"); } // Print footer if necessary // Print the header if the graph is printed alone independently and not // in the context for example of a multigraph printing if (!m_options.IsSet(ThompsonOptions.TO_COMBINEGRAPHS)) { graphvizStringBuilder.Append("}\r\n"); } return(graphvizStringBuilder); }