Application of Small algorithms and application Algorithms

Source: Internet
Author: User

Application of Small algorithms and application Algorithms

Recently I am working on a small WPF project. I have to write a function because I need to process some data. It is a small algorithm. I want to write it here to help me learn new things. At the same time, I hope the cool guys will give me some advice and find out some shortcomings.

 

Simplified requirements:

Create a datatable table dt. The number of records in the dt table is m. Create able tables dt1, dt2, dt1, and dt2 to display the data in dt cyclically. Add a timer to regularly refresh the data in dt1 and dt2, and dt1 and dt2 can only display n rows at a time.

 

1. Declare Variables

Private DispatcherTimer timer; DataTable dt = new DataTable (); DataTable dt1 = new DataTable (); DataTable dt2 = new DataTable (); public static int currentRows = 0; // The current row public static int currentRows2 = 0; // intermediate variableView Code

Timer:

Timer = new DispatcherTimer (); timer. Interval = new TimeSpan (0, 0, 5); timer. Tick + = new EventHandler (timer_Tick); timer. Start ();View Code

 

2. Add a control. Here, add two datagrid controls to display data in dt1 and dt2)

<DataGrid AutoGenerateColumns = "False" Height = "335" HorizontalAlignment = "Left" Margin = ", 12, 200 "Name =" dataGrid1 "verticalignment =" Top "Width =" "> <DataGrid. columns> <maid Header = "id" Binding = "{Binding Path = id}"> </maid> <maid Header = "name" Binding = "{Binding Path = name} "> </DataGridTextColumn> </DataGrid. columns> </DataGrid> <DataGrid AutoGenerateColumns = "False" Height = "305" HorizontalAlignment = "Left" Margin = "463,31, 200 "Name =" dataGrid2 "verticalignment =" Top "Width =" "> <DataGrid. columns> <maid Header = "id" Binding = "{Binding Path = id}"> </maid> <maid Header = "name" Binding = "{Binding Path = name} "> </DataGridTextColumn> </DataGrid. columns> </DataGrid>View Code

 

3. Create a dt able table dt and initialize dt1 and dt2.

private void Initialize(int num)        {            dt.Columns.Add(new DataColumn("id", typeof(int)));            dt.Columns.Add(new DataColumn("name", typeof(string)));            for (int i = 0; i < num; i++)            {                DataRow dr = dt.NewRow();                dr["id"] = i;                dr["name"] = "tom" + i.ToString();                dt.Rows.Add(dr);            }            dt1.Columns.Add(new DataColumn("id", typeof(int)));            dt1.Columns.Add(new DataColumn("name", typeof(string)));            dt2.Columns.Add(new DataColumn("id", typeof(int)));            dt2.Columns.Add(new DataColumn("name", typeof(string)));        }

Call at initialization:

Initialize (11 );

 

4. Compile Functions

public void Update(int num)        {            int rows = dt.Rows.Count;            int num1 = 0;            int num2 = 0;            currentRows = currentRows2;            for (int i = currentRows; i < (currentRows + num); i++)            {                num1++;                currentRows2++;                DataRow dr = dt1.NewRow();                dr["id"] = Convert.ToInt32(dt.Rows[i][0].ToString());                dr["name"] = dt.Rows[i][1].ToString();                dt1.Rows.Add(dr);                if (i == rows - 1)                {                    i = -1;                    currentRows2 = 0;                }                if (num1 == num)                {                    break;                }            }            currentRows = currentRows2;            for (int i = currentRows; i < (currentRows + num); i++)            {                num2++;                currentRows2++;                DataRow dr = dt2.NewRow();                dr["id"] = Convert.ToInt32(dt.Rows[i][0].ToString());                dr["name"] = dt.Rows[i][1].ToString();                dt2.Rows.Add(dr);                if (i == rows - 1)                {                    i = -1;                    currentRows2 = 0;                }                if (num2 == num)                {                    break;                }            }        }

 

5. Regularly refresh data in dt1 and dt2

void timer_Tick(object sender, EventArgs e)        {            dt1.Clear();            dt2.Clear();            Update(3);                    }

 


A * Practical Significance of Algorithm Application

A * algorithm is A typical Heuristic Search Algorithm in artificial intelligence. To clarify A * algorithm, let me first talk about it.

I. What is a heuristic search algorithm?

Before you start, you need to search for the status space. Status space search. If it is based on a professional point, the problem solving process is represented by the process of searching for this path from the initial state to the target State. In layman's terms, when solving a problem, the process of finding a problem can start from the problem to the result of the problem (as though not popular ). There are many branches in the process of solving the problem, mainly because of the uncertainty of the conditions for solving the problem in the process of solving the problem, which is caused by incompleteness. This constitutes a picture of the many paths for solving the problem, we say this figure is the state space. The problem is actually solved by finding a path in this figure from the beginning to the result. The search process is the state space search.

Frequently Used status space searches have priority in depth and breadth. Breadth First is to find the target layer by layer until the target is found. Depth first searches for one branch in a certain order, and then finds another branch until the target is found. These two algorithms are described in the data structure book. You can refer to these books for more detailed explanations.

One major drawback of the breadth and depth preference search mentioned above is that they all work in a given State Space. This is a suitable algorithm when the state space is not large, but it is not advisable when the state space is large and unpredictable. His efficiency is too low to be completed. Heuristic Search is required here.

Heuristic Search is to evaluate the position of each search in the state space to obtain the best position, and then search until the target. In this way, a large number of fearless search paths can be omitted, and efficiency is mentioned. In heuristic search, location estimation is very important. Different estimates can have different effects. Let's first look at how the valuation is expressed.

In the inspiration, the valuation is represented by the valuation function, for example:

F (n) = g (n) + h (n)

Where f (n) is the estimated function of node n, the actual cost of g (n) from the initial node to the n node in the real state space, h (n) it is the estimated cost of the optimal path from n to the target node. Here, h (n) mainly reflects the inspiration information of search, because g (n) is known. For details, g (n) indicates the priority of search breadth. However, when h (n)> g (n), g (n) can be omitted to improve efficiency. This is deep, and it will not be affected if you don't understand it! Let's continue to look at what A * algorithm is.

Ii. First recognized A * Algorithm

There are actually many heuristic search algorithms, such as local preferred search and best preferred search. Of course, A * is also. These algorithms use heuristic functions, but they have different policies when selecting the best search node. The local preferred search method is to select the "best node" in the search process and discard other sibling nodes. Father's Day points have to be searched continuously. The search results are obvious. Because other nodes are discarded, the best nodes may also be discarded, because the best node for solving is the best at this stage, not necessarily the global best. It is better to give priority to a more intelligent node. When searching, the node is not discarded (unless the node is a dead node ), in each step of evaluation, compare the current node with the previous node's estimated value to get an "Best node ". This effectively prevents the loss of "best node. So what kind of algorithm is A * algorithm? In fact, the * algorithm is also the best priority algorithm. Only some constraints need to be added. When solving some problems, we hope to be able to solve the Shortest Path of the state space search, that is, to solve the problem using the shortest method. A * is doing this! Let's define it first. If an estimate function can find the shortest path, we call it adoption. A * algorithm is the best priority algorithm that can be adopted. The evaluation function of A * algorithm can be expressed:

F' (n) = G' (n) + H' (n)

Here, f' (n) is the evaluation function, and G' (n) is the shortest path value from the start point to the end point, H' (n) it is the inspiration value from n to the target's most Broken Circuit. Because f' (n) is actually unable to pre-Wait... the remaining full text>

Application of sorting algorithms

Fast sorting
Procedure sort (l, r: integer );
Var I, j, x, y: integer;
Begin
I: = l; j: = r;
X: = a [l];
Repeat
While a [I] <x do inc (I );
While x <a [j] do dec (j );
If I <= j then begin
Y: = a [I];
A [I]: = a [j];
A [j]: = y;
Inc (I); dec (I );
End;
Until I> j;
If I <r then sort (I, r );
If l <j then sort (l, j );
End;
Pascal's sorting program segment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.