コード例 #1
0
        public static void buildGraphFromInitialState(IDirectedGraph <char> graph, string rawGraphData)
        {
            try
            {
                string[] verticeEdgeData = rawGraphData.Split(",");

                foreach (string verticeEdge in verticeEdgeData)
                {
                    string verticeEdgeTrimmed = verticeEdge.Trim();

                    char  vertecie1 = Char.Parse(verticeEdgeTrimmed.Substring(0, 1));
                    char  vertecie2 = Char.Parse(verticeEdgeTrimmed.Substring(1, 1));
                    float weight    = float.Parse(verticeEdgeTrimmed.Substring(2, verticeEdgeTrimmed.Length - 2));

                    graph.AddVertex(vertecie1);
                    graph.AddVertex(vertecie2);

                    graph.AddEdge(vertecie1, vertecie2, weight);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("The graph data was passed in an incorrect form.\n\tFor a node A that connects to a node B with a weight of 5 pass data in like 'AB5'.\n\tSeparate data by columns: 'AB5, AC2'");
                throw;
            }
        }
コード例 #2
0
ファイル: NFA.cs プロジェクト: ddayzzz/Algorithms-In-CSharp
        private void doDFA_multiOr(string regexp)
        {
            Stack <int> ops = new Stack <int>();

            re = regexp.ToArray();
            M  = re.Length;
            G  = new DirectedWeightedGraph(M + 1);
            for (int i = 0; i < M; ++i)
            {
                int lp = i;
                if (re[i] == '(' || re[i] == '|')
                {
                    //左括号或者或运算符
                    ops.Push(i);
                }
                else if (re[i] == ')')
                {
                    //可能顶部会有 or 运算符
                    int        tc           = ops.Pop();
                    List <int> listOfOrLast = new List <int>();
                    while (re[tc] == '|')
                    {
                        G.AddEdge(tc, i);
                        listOfOrLast.Add(tc + 1);
                        tc = ops.Pop();
                    }
                    // 最后一次的 tc 对应的位置是 左括号。
                    for (int k = 0; k < listOfOrLast.Count; ++k)
                    {
                        G.AddEdge(tc, listOfOrLast[k]);
                    }
                    // lp 需要更新为下一次处理的
                    lp = tc;
                }
                if (i < M - 1 && re[i + 1] == '*') //查看下一个字符
                {
                    G.AddEdge(lp, i + 1);          //这个字符是可以>=0的,双向状态转换
                    G.AddEdge(i + 1, lp);
                }
                if (re[i] == '(' || re[i] == '*' || re[i] == ')')
                {
                    G.AddEdge(i, i + 1);//后面的状态
                }
            }
        }
コード例 #3
0
ファイル: NFA.cs プロジェクト: ddayzzz/Algorithms-In-CSharp
        private void doDFA_normal(string regexp)
        {
            Stack <int> ops = new Stack <int>();

            re = regexp.ToArray();
            M  = re.Length;
            G  = new DirectedWeightedGraph(M + 1);
            for (int i = 0; i < M; ++i)
            {
                int lp = i;
                if (re[i] == '(' || re[i] == '|')
                {
                    //左括号或者或运算符
                    ops.Push(i);
                }
                else if (re[i] == ')')
                {
                    int or = ops.Pop();
                    if (re[or] == '|')
                    {
                        //如果右括号,而且之前是或运算符的右边部分
                        lp = ops.Pop();        //需要建立某个双向的连接,
                        G.AddEdge(lp, or + 1); //从左括号到或运算符后(不满足的一个)
                        G.AddEdge(or, i);      //从左括号到右括号
                    }
                    else
                    {
                        lp = or;//如果仅仅是一组字符串而已用()括住的话。lp 设置为左括号的位置。因为括号的优先级是很高的。
                    }
                    //所以在栈上括号在 * 顶部
                }
                if (i < M - 1 && re[i + 1] == '*') //查看下一个字符
                {
                    G.AddEdge(lp, i + 1);          //这个字符是可以>=0的,双向状态转换
                    G.AddEdge(i + 1, lp);
                }

                if (re[i] == '(' || re[i] == '*' || re[i] == ')')
                {
                    G.AddEdge(i, i + 1);//后面的状态
                }
            }
        }
コード例 #4
0
ファイル: Graph.cs プロジェクト: lanicon/Komaci
        public static void AddGraphReversed(
            IDirectedGraph <V, E> destination,
            IDirectedGraph <V, E> source)
        {
            AddAllVertices(destination, source.Vertices);

            foreach (var edge in source.Edges)
            {
                destination.AddEdge(edge.Target, edge.Source);
            }
        }
コード例 #5
0
        private void InitializePatternGraph()
        {
            using (Trace.Entering())
            {
                // Defines how patterns connect grammar characters.
                PatternGraph = new DirectedGraph <GrammarCharacter, Pattern>();
                PatternGraph.AddVertices(GrammarCharacterExt.GetValues());

                foreach (Pattern pattern in Patterns)
                {
                    // If the pattern connects two grammar characters.
                    bool isEdge = pattern.LeftRule.GrammarCharacter != GrammarCharacter.e && pattern.RightRule.GrammarCharacter != GrammarCharacter.e;
                    if (isEdge)
                    {
                        PatternGraph.AddEdge(pattern.LeftRule.GrammarCharacter, pattern.RightRule.GrammarCharacter, pattern);

                        if (pattern.LeftRule.GrammarCharacter != pattern.RightRule.GrammarCharacter)
                        {
                            PatternGraph.AddEdge(pattern.RightRule.GrammarCharacter, pattern.LeftRule.GrammarCharacter, pattern);
                        }
                    }
                }
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: kdushin/CSharpPractice
        private static void TestDirectedGraph(string path)
        {
            using (var reader = new StreamReader(path))
            {
                IDirectedGraph <int> graph = GraphHelper.CreateWithCapacity(8);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        continue;
                    }
                    graph.AddEdge(int.Parse(line.Split()[0]), int.Parse(line.Split()[1]));
                }

                Console.WriteLine(graph);
                //Console.WriteLine(graph.ReverseGraph());
            }
        }
コード例 #7
0
ファイル: KnowledgeBase.cs プロジェクト: Zakifull/DTU
        public void AddClause(IClause clause)
        {
            if (clause == null)
            {
                return;
            }
            _satisfiedLiterals.Clear();

            if (clause.Head != null && clause.Head.Negated && clause.Body.Count > 0)
            {
                var newClause = new Clause(new Literal(clause.Head.NegatedValue()));
                foreach (var literal in clause.Body)
                {
                    newClause.Body.Add(new Literal(literal.NegatedValue()));
                }
                AddClause(newClause);
                return;
            }

            var headNode = clause.Head != null ? (_graph.GetNode(clause.Head.Value) ?? _graph.AddNode(clause.Head.Negated ? (ILogicNode) new ClauseNode(clause.Head.Value) : new LiteralNode(clause.Head.Value)).GetNode(clause.Head.Value)) : null;

            if (clause.Body.Count < 1)
            {
                if (headNode == null)
                {
                    throw new NullReferenceException("Clause Head is null");
                }
                headNode.Fact = true;
            }
            else if (clause.Body.Count == 1)
            {
                if (clause.Head == null)
                {
                    throw new NullReferenceException("Clause Head is null");
                }
                var literal = clause.Body.First();

                if (clause.Head.Value != literal.Value)
                {
                    var node = _graph.GetNode(literal.Value) ?? _graph.AddNode(literal.Negated ? (ILogicNode) new ClauseNode(literal.Value) : new LiteralNode(literal.Value)).GetNode(literal.Value);

                    if (_graph.GetEdge(headNode, node) == null)
                    {
                        _graph.AddEdge(new DirectedEdge <ILogicNode> (headNode, node));
                    }
                }
            }
            else if (clause.Body.Count > 1)
            {
                if (clause.Head != null)
                {
                    var literals = clause.Body.Where(literal => clause.Head.Value != literal.Value).ToList();
                    if (literals.Count > 0)
                    {
                        var clauseKey  = ClauseKey(literals);
                        var clauseNode = _graph.GetNode(clauseKey) ?? _graph.AddNode(new ClauseNode(clauseKey)).GetNode(clauseKey);

                        if (_graph.GetEdge(headNode, clauseNode) == null)
                        {
                            _graph.AddEdge(new DirectedEdge <ILogicNode> (headNode, clauseNode));
                        }

                        foreach (var literalNode in literals.Select(literal => _graph.GetNode(literal.Value) ?? _graph.AddNode(literal.Negated ? (ILogicNode) new ClauseNode(literal.Value) : new LiteralNode(literal.Value)).GetNode(literal.Value)).Where(literalNode => _graph.GetEdge(clauseNode, literalNode) == null))
                        {
                            _graph.AddEdge(new WeightedDirectedEdge <ILogicNode> (clauseNode, literalNode, 1));
                        }
                    }
                }
                else
                {
                    var disjointLiteralKey = DisjointLiteralKey(clause.Body);
                    var disjointLiteral    = _graph.GetNode(disjointLiteralKey) ?? _graph.AddNode(new LiteralNode(disjointLiteralKey)).GetNode(disjointLiteralKey);

                    foreach (var literalNode in clause.Body.Select(literal => _graph.GetNode(literal.Value) ?? _graph.AddNode(literal.Negated ? (ILogicNode) new ClauseNode(literal.Value) : new LiteralNode(literal.Value)).GetNode(literal.Value)).Where(literalNode => _graph.GetEdge(disjointLiteral, literalNode) == null))
                    {
                        _graph.AddEdge(new DirectedEdge <ILogicNode>(disjointLiteral, literalNode));
                    }
                }
            }

            var negated = clause.Head != null ? new Clause(new Literal(clause.Head.NegatedValue())) : new Clause();

            foreach (var dependency in clause.Body)
            {
                negated.Body.Add(new Literal(dependency.NegatedValue()));
            }
            AddNegatedClause(negated);

            if (headNode != null && negated.Head != null)
            {
                var negatedHeadNode = _graph.GetNode(negated.Head.Value);
                if (negatedHeadNode != null)
                {
                    if (headNode.Fact && negatedHeadNode.Fact)
                    {
                        throw new Exception("KB is inconsistent, " + headNode.Id + " and " + negatedHeadNode.Id + " are both facts!");
                    }

                    if (clause.Body.Count > 0)
                    {
                        var nodesA = clause.Body.Select(literal => _graph.GetNode(literal.Value));
                        var nodesB = clause.Body.Select(literal => _graph.GetNode(literal.NegatedValue()));
                        if (SharedAncestor(nodesA, nodesB))
                        {
                            Console.WriteLine("Adding " + clause.ToProperString() + " will cause conflict skipping clause...");
                            RemoveClause(clause);
                        }
                    }
                }
            }

            if (headNode != null && headNode.Fact)
            {
                FactFlow(headNode);
            }
            if (clause.Body.Count > 0)
            {
                foreach (var node in clause.Body.Select(literal => _graph.GetNode(literal.Value)).Where(node => node.Fact))
                {
                    FactFlow(node);
                }

                foreach (var node in clause.Body.Select(literal => _graph.GetNode(literal.NegatedValue())).Where(node => node.Fact))
                {
                    FactFlow(node);
                }
            }
        }