예제 #1
0
        public static bool CheckMarkovTheorem(Code code)
        {
            OrientedGraph graph     = new OrientedGraph();
            GraphNode     startNode = new GraphNode(String.Empty);

            graph.AddNode(startNode);
            foreach (var codeword in code.ElementaryCodes.Values)
            {
                for (int i = 0; i < codeword.Length; i++)
                {
                    string         prefix    = codeword.Substring(0, i);
                    string         tail      = codeword.Substring(i);
                    List <string>  postfixes = new List <string>();
                    Queue <string> tails     = new Queue <string>();
                    tails.Enqueue(tail);
                    while (tails.Count > 0)
                    {
                        string currentTail = tails.Dequeue();
                        foreach (var middleWord in code.ElementaryCodes.Values)
                        {
                            if (middleWord.Length <= currentTail.Length && codeword != middleWord &&
                                currentTail.Substring(0, middleWord.Length) == middleWord)
                            {
                                string newTail = currentTail.Substring(middleWord.Length);
                                tails.Enqueue(newTail);
                                postfixes.Add(newTail);
                            }
                        }
                    }

                    if (postfixes.Count == 0)
                    {
                        continue;
                    }

                    GraphNode prefixNode = graph.GetNode(prefix);
                    if (prefixNode == null)
                    {
                        prefixNode = new GraphNode(prefix);
                        graph.AddNode(prefixNode);
                    }
                    foreach (var postfix in postfixes)
                    {
                        if (postfix != codeword)
                        {
                            GraphNode postfixNode = graph.GetNode(postfix);
                            if (postfixNode == null)
                            {
                                postfixNode = new GraphNode(postfix);
                                graph.AddNode(postfixNode);
                            }
                            prefixNode.Connections.Add(postfixNode);
                        }
                    }
                }
            }
            return(!graph.CycleWithNodeExists(startNode));
        }
예제 #2
0
        /// <summary>
        /// fields dependencies graph
        /// </summary>
        private OrientedGraph CreateFieldsGraph(Dictionary <String, FieldInfo> fields)
        {
            OrientedGraph FieldsGraph = new OrientedGraph();     // граф зависимости полей сделки

            // add graph nodes
            foreach (var field in fields.Keys)
            {
                FieldsGraph.AddNode(field);

                // add graph transitions
                foreach (var fieldList in fields.Values)
                {
                    // nH. Пропустить ребра, направленные в поле, которое изменилось.
                    //if (startField != null && !startField.Equals(field.Name, StringComparison.Ordinal))

                    foreach (var param in fieldList.lParamInfo.Where(p => !p.IsDeleted))
                    {
                        if (param != null && FieldsGraph.ContainsNode(param.FieldName))
                        {
                            FieldsGraph.AddInTrans(field, param.FieldName);
                        }
                    }
                }
            }
            return(FieldsGraph);
        }
예제 #3
0
        public static FrequencyCode GetOptimalCode(string alphabet, Dictionary <char, double> frequencyList)
        {
            int q = frequencyList.Count % (alphabet.Length - 1);

            if (q == 0)
            {
                q = alphabet.Length - 1;
            }
            if (q == 1)
            {
                q = alphabet.Length;
            }
            List <NodeContent> looseVertices = new List <NodeContent>();
            OrientedGraph      graph         = new OrientedGraph();

            foreach (var item in frequencyList)
            {
                NodeContent content = new NodeContent(item.Key, item.Value);
                AddToListWithSort(looseVertices, content);
                graph.AddNode(new GraphNode(content));
            }

            while (looseVertices.Count > 1)
            {
                NodeContent newContent = new NodeContent();
                GraphNode   newNode    = new GraphNode(newContent);
                for (int i = 0; i < q; i++)
                {
                    NodeContent content = looseVertices[0];
                    newContent.Frequency += content.Frequency;
                    GraphNode node = graph.GetNode(content);
                    newNode.Connections.Add(node);
                    looseVertices.RemoveAt(0);
                }
                AddToListWithSort(looseVertices, newContent);
                graph.AddNode(newNode);
                q = alphabet.Length;
            }
            FrequencyCode     code             = new FrequencyCode();
            Queue <GraphNode> broadSearchQueue = new Queue <GraphNode>();

            broadSearchQueue.Enqueue(graph.GetNode(looseVertices[0]));

            while (broadSearchQueue.Count > 0)
            {
                GraphNode   node    = broadSearchQueue.Dequeue();
                NodeContent content = (NodeContent)node.NodeContent;
                if (node.Connections.Count == 0)
                {
                    code.Set(content.Symbol, content.Code, content.Frequency);
                }
                else
                {
                    for (int i = 0; i < node.Connections.Count; i++)
                    {
                        NodeContent childContent = (NodeContent)node.Connections[i].NodeContent;
                        childContent.Code = content.Code + alphabet[i];
                        broadSearchQueue.Enqueue(node.Connections[i]);
                    }
                }
            }
            return(code);
        }