In this section, the methodology of producing final tables is described step by step based on WDL. In a general outline, work begins with producing an initial final table. The initial final table is a table with the number of tournament teams lined up, and all rows have zero values for WDL. Then all possible and valid cases for the first row are calculated and added to the initial table. As each mode is added, a new table is created. Because the valid WDL modes for the first row are more than one, the number of final tables generated after adding the first-row modes will be more than one table. The same operation is repeated for all other rows of all existing tables, and each time new tables are generated. In the end, all of the generated tables can be considered as final tables. The general flowchart of the proposed method is shown in Fig. 1.
First, an initial blank table is created to design the final tables. The initial table is a table that has a row with the number of teams, and in each row, the values of all columns L, D, and W are zero. This table is passed to a function that calculates all possible states for the first row and creates a new table for each state that contains the possible states in the first row. By inserting each mode in the first row, its effects are applied in the next rows. For example, if the mode is equal to (2,0,1), the effect of this state in the next rows will be one win and two losses. Therefore, since it is unclear which rows win and which rows lose, each situation must create separate tables. Each table's order should not change during the operation so that the score of each row should be equal to or higher than the row. At the end of the first-row operation, the corresponding operation is performed in the same way for the next rows for each obtained table. After operating for the last row, all the resulting tables will answer the problem.
In the following, the sections that can be useful for solving the problem are stated, and then the implementation done with one of the functional programming languages is given.
Production of possible WDLs
Each team in a tournament can achieve one of three results: win, draw and lose. Regardless of the order of results, the number of each type of result can determine the team's overall result. Eq. 6 can easily obtain the points earned by each team. We can consider different approaches to obtain all possible scenarios from the results. The simplest approach can consıder all possible situations and select only the modes allowed in Eq. 5. List 2 shows the proper code for the first approach to this topic in the Haskell programming language.
Algorithm resultStates( n ):
retList = []
For W = 0 to n-1:
For D = 0 to n-1:
For L = 0 to n-1:
If W + D + L = = n-1: Add (W, D, L) to retList
return retList
|
List 2. first version of resultStates algorithm for generating all possible result states of each team. |
The resultStates function in List 2 receives input as the number of teams and returns all possible result scenarios that one team can achieve against the other teams. According to the resultStates function to obtain \(\frac{n*(n-1)}{2}\) correct state, \((n-1{)}^{3}\) states are examined. By applying changes to the above function and considering the impact values of each section in subsequent sections, the function listed in List 3 can reduce the number of studies.
Algorithm resultStates( n ):
retList = []
For W = 0 to n-1:
For D = 0 to n-W-1:
For L = 0 to n-W-D-1:
If W + D + L = = n-1: Add (W, D, L) to retList
return retList
|
List 3. modified version of resultStates algorithm for generating all possible result states of each team. |
The function expressed in List 3 examines fewer modes than the function expressed in List 2. The total number of states studied in this function equals Eq. 10.
$$1+3+6+10+15+\dots +\frac{n*\left(n+1\right)}{2}=\frac{n*(n+1)*(n+2)}{3!}$$
10
Of course, the solution from Greg Cooperberg's idea seems to be a better solution. In this solution, for w, all possible states (0 .. n-1) are selected, and for each w state (0..n-w-1) is considered for d, and the value of L can be calculated by relation (L = n - w - d -1). The Haskell code for this solution is given in List 4.
Algorithm resultStates( n ):
retList = []
For W = 0 to n-1:
For D = 0 to n-W-1:
Add (W, D, n-W-d-1) to retList
return retList
|
List 4. last version of resultStates algorithm for generating all possible result states of each team. |
In this case, the number of events equals the number of cases and is considered the optimal solution.
Generating a set of game results that can gain a WDL for a team.
The result of a game affects both teams. Therefore, the results of a tournament for one team in a particular situation affect other teams. This section examines all the effects of one team's situations on other teams. The effects will be in the form of tables, each row related to the game's result with a team.
A team must compete in w + d + l for a specific WDL. If the order of the teams is also important, Eq. 11 can obtain the number of possible modes to generate a particular WDL.
$${P}_{wdl}\left(T\right)=\frac{n!}{w!d!l!}=\frac{(W+D+L)!}{W!D!L!}$$
11
For example, three different permutations for the state (2,0,1) could be as follows:
$$\left\{\left(\begin{array}{c}(\text{1,0},0)\\ (\text{1,0},0)\\ (\text{0,0},1)\end{array}\right),\left(\begin{array}{c}(\text{1,0},0)\\ (\text{0,0},1)\\ (\text{1,0},0)\end{array}\right),\left(\begin{array}{c}(\text{0,0},1)\\ (\text{1,0},0)\\ (\text{1,0},0)\end{array}\right)\right\}$$
According to the example above, the possible scenarios are:
-
Win over the first and second teams and lose to the third team.
-
Win over the first and third teams and lose to the second team.
-
Win over the second and third teams and lose to the first team.
We can use the tree method to produce different compounds. The algorithm starts by creating a set of elements labeled W, D, L according to the problem values as the initial set. A tree is then created whose root is denoted by R and contains the initial set. Then, child nodes for each element of the set (only once per member, if any) are created, labeled with the corresponding element value. Each child node has its own parent set from which the value of the node element has been removed. The above operation is repeated for each new node created. This process continues until all the sets in the leaves are empty. For example, we can do the following to get all the compounds of state (2,1,1):
First, a set of elements is formed as {W, W, D, L} and by assigning it to the root, the corresponding tree is created. Due to all three modes in the initial set, three child nodes with labels w, d, and l are created, and appropriate sets are assigned. The corresponding tree can be seen in Fig. 2.
The proposed algorithm is implemented on sets of leaves, and suitable children are created for them. Relevant sets are also calculated for each leaf. Figure 3 shows the problem tree for the second level.
With the complete implementation of the algorithm, the final tree is created according to Fig. 4. In the final, all the leaves have empty sets.
After creating the tree, each path from the root to the tree's leaves is a permutation of the desired set. A set of all these paths will serve as the answer. In the example given, the set of permutations according to the tree in Fig. 4 is as follows:
Implementing in Haskell
The method described in this research is based on functional programming. As one of the functional programming languages, Haskell can be used to implement the proposed method. The Haskell language is a set of functions, usually programming as recursive functions.
This section deals with implementing the proposed method using the Haskell language. Here, a need is to define a new structure as a recursive data structure. The proposed method presents the following tree data structure.
We can use the following process to create an empty table for n teams:
$$\left[\right(\text{0,0},0\left) \right| x \leftarrow [1 ... n]]$$
This function creates a list containing n topples with values (0,0,0).
The program starts with calling a function named findTables. This function has an input that specifies the number of tournament teams. The output of the finalTables function is the final table set, which is considered the answer to the problem. This function has been developed to implement the non-recursive part of the proposed method. The recursive part is implemented by a function called stage'. The finalTables function returns the results of the stage' function. Also, in the finalTables function, the stageN function is called. The initial table has been sent for this function, and the function applies all possible scenarios for the first row in the initial table. The tables created are passed from the stageN function to the stage' function. So, stage' is repeated for the second row to the end. Here the stageList function generates a list of all possible states for a specific row of tables. We will examine this function in a separate section. The Haskell code of described functions is as follows:
$$finalTables :: int \to \left[\left[Row\right]\right]$$
$$finalTables 0 = \left[\right[\left]\right]$$
$$finalTables n = stag{e}^{\text{'}} list1 n 1$$
$$where list1 = stageN\left(\left[\text{0,0},0\right]|x\leftarrow \left[1\cdots n\right]\right]) \left(stageList n 0 \left(n,\text{0,0}\right)\left(\text{0,0},0\right)\right) 1$$
$$stag{e}^{\text{'}} :: \left[\left[Row\right]\right]\to Int \to int \to \left[\left[Row\right]\right]$$
$$stag{e}^{\text{'}} \left[\right] \_ \_ = \left[\right]$$
$$stag{e}^{\text{'}} \left(xs:xss\right) n r = rmdups (tbls++(stag{e}^{\text{'}} xss n r)$$
$$where tbls = oneState xs n (r+ 1 )$$
$$oneState :: \left[Row\right] \to int\to Int \to \left[\left[Row\right]\right]$$
$$|r==n+1 = \left[xs\right]$$
$$| otherwise = stag{e}^{\text{'}} \left(stageN xs \left(stageList n r \left(xs‼\left(r-2\right)\right)\left(xs‼\left(r-1\right)\right)\right)r\right) n r$$
$$stageN :: \left[Row\right] \to \left[Row\right]\to Int \to \left[\left[Row\right]\right]$$
$$stageN \_ \left[\right] \_ = \left[\right]$$
$$stageN xs \left[\right(\text{0,0},0\left)\right] \_ = \left[xs\right]$$
$$stageN xs \left(y:ys\right) r = \left(add \left(r-1\right) xs \left(permutation y\right)\right)++stageN xs ys r$$
The functions listed above form the main body of the program. These functions implement the proposed method by calling each other. Table 5 shows the list of functions of the main part of the program with the necessary explanations.
Table 5
Haskell functions for main section
Function Name
|
Description
|
Input Parameters
|
Output
|
finalTables
|
The main function
|
Number of teams
|
Set of all final tables
|
stage'
|
Apply operations related to the method provided on all rows of the table.
|
A set of tables, number of teams, and the desired row
|
Final and intermediate tables
|
oneState
|
It is a stage's auxiliary function responsible for performing operations on a row.
|
A table, number of teams, and desired row
|
Modified table
|
stageN
|
It is responsible for adding a state to a row and applying its effects to other rows.
|
A table to be added, Source tables, and desired row
|
Modified table
|
In the functions listed in Table 5, the permutation, stageList, and add functions are also used to obtain different combinations of results, a list of all possible scenarios that a team can achieve, and the addition of a table to another table, respectively.
The permutation function obtains a list of required tables by calling formTree and readTree functions. The formTree and readTree functions are also used to create and trace trees according to the content described in the previous section. The code list for creating combinations from the tree method is given below.
$$formTree :: \left[Row\right] \to \left[Tree\right]$$
$$formTree xs = map\left(\text{'}formNod{e}^{\text{'}}xs\right)\left(filter \left(\text{'}ele{m}^{\text{'}}xs\right)\right[\left(\text{1,0},0\right),\left(\text{0,1},0\right),\left(\text{0,0},1\right)]$$
$$formNode :: \left[Row\right] \to \left[Row\right]\to Int \to \left[\left[Row\right]\right]$$
$$\left|length\right(xs)==1 = Node y []$$
$$| otherwise = Node y (formTree \left(xs \backslash \backslash \left[y\right]\right))$$
$$readTree :: \left[Tree\right] \to \left[\right[Row\left]\right]$$
$$readTree xs = foldr \left( \backslash x y \to readTre{e}^{\text{'}}x++y\right) \left[\right] xs$$
$$readTree\text{'} :: Tree \to \left[\right[Row\left]\right]$$
$$readTre{e}^{\text{'}} \left(Node x \left[\right]\right) =\left[\left[x\right]\right]$$
$$readTre{e}^{\text{'}} \left(Node x xs\right) = map\left(\backslash y \to \left[x\right]++y\right) \left(foldr\left(\backslash x y\to readTre{e}^{\text{'}} x++y\right) \right[\left] xs\right)$$
$$permutation :: Row \to \left[\left[Row\right]\right]$$
$$permutation \left(w,d,l\right) = map\left(\backslash xs \to \left(w,d,l\right):xs\right) \left(readTree \left(formTree \left(setEleman \left(l,d,w\right)\right)\right)\right)$$
$$where setEleman \left(w,d,l\right)= \left[\left(\text{1,0},0\right)|x\leftarrow \left[1..w\right]\right]++\left[\left(\text{0,1},0\right)|x\leftarrow \left[1..d\right]\right]++\left[\left(\text{0,0},1\right)|x\leftarrow \left[1..l\right]\right]$$
Another function used in the proposed program is the add function. This function is used to place a row of a table in the corresponding row of other tables and add the values of the other rows of a table to the corresponding values in other tables. The add function inserts the rth row of the table at the beginning of the tables returned from the addTb function. The addTb function adds the values of the two tables sent using an operator called (! +).
For example, suppose a tournament with four teams. For this tournament, in the first row, consider the probable state (1,0,2). According to the mentioned features, the effects of this state are equal to:
$$\left\{\left(\begin{array}{c}(\text{1,0},0)\\ (\text{1,0},0)\\ (\text{0,0},1)\end{array}\right),\left(\begin{array}{c}(\text{1,0},0)\\ (\text{0,0},1)\\ (\text{1,0},0)\end{array}\right),\left(\begin{array}{c}(\text{0,0},1)\\ (\text{1,0},0)\\ (\text{1,0},0)\end{array}\right)\right\}$$
So, this state can create the following tables from the initial blank table.
$$\left(\left(\begin{array}{c}(\text{2,0},1)\\ (\text{1,0},0)\\ (\text{1,0},0)\\ (\text{0,0},1)\end{array}\right),\left(\begin{array}{c}(\text{2,0},1)\\ (\text{1,0},0)\\ (\text{0,0},1)\\ (\text{1,0},0)\end{array}\right),\left(\begin{array}{c}(\text{2,0},1)\\ (\text{0,0},1)\\ (\text{1,0},0)\\ (\text{1,0},0)`\end{array}\right)\right)$$
The list of code for add and addTb functions is given below.
$$add :: Int \to \left[Row\right] \to \left[\left[Row\right]\right] \to \left[\left[Row\right]\right]$$
$$add r xs \left(yss\right) = \left[take r xs++addTb \left(drop r xs\right) ys \right| ys \leftarrow yss$$
$$addTb :: \left[Row\right] \to \left[Row\right] \to \left[Row\right]$$
$$addTb xs ys = \left[x !+y \right| \left(x, y\right)\leftarrow zip xs ys$$
$$where \left(!+\right) \left(w1,d1,l1\right) \left(w1,d1,l1\right) = (w1+w2, d1+d2, l1+l2)$$
Another function used in the proposed program is the stageList function. This function generates all possible states for a row. In this function, the values of the effects of the previous lines must be considered. So, we can create possible states according to the previous, and the current state can be subtracted. We should remove states with negative values from the list of states. As it turns out, this method of state generation may not be appropriate, as it produces invalid states. Generating and then deleting invalid states will increase the complexity of the program. The following is an efficient way to produce this type of case.
Improved production of possible states.
We can place limitations rules on generating possible states of a row to optimize the table production process. Restrictions should destroy the possibility of producing invalid states. Thus, we can improve the efficiency of this part of the algorithm.
In a tournament with n teams, one can win all of their games at best, i.e., status (n-1,0,0). This team will be the first team at the table. In the worst case, the first team can also happen (0, n-1,0). This situation happens when all the teams have achieved the same result. On the other hand, the team that loses all its games will be in the last row of the table as the worst possible situation, i.e. (0, 0, n-1). In the best case, this team can achieve the maximum result \(\left(⌊\frac{n-1}{2}⌋,(n-1)\%2,⌊\frac{n-1}{2}⌋\right)\)).
For example, in a four-team tournament, the first team can score (3,0,0) at best and (0,3,0) at worst. Also, the last team can get the result (0,3,0) in the best case and (0,0,3) in the worst case.
Now suppose the final table is divided into two pieces as follows:
$$\left(\begin{array}{c}1\\ 2\\ \begin{array}{c}⋮\\ k\\ ⋮\end{array}\\ n\end{array}\right)=\begin{array}{c}\left(\begin{array}{c}1\\ 2\\ \begin{array}{c}⋮\\ k\end{array}\end{array}\right)\\ \left(\begin{array}{c}k\\ ⋮\\ \begin{array}{c}⋮\\ n\end{array}\end{array}\right)\end{array}$$
Team k is the last team in the first part, and the first team is in the second part. Team k is the last team in the first part and is the first team in the second part. Minimum and maximum values can be obtained for team k. For this purpose, suppose \({P}_{1,max}^{k}\) and \({P}_{2,max}^{k}\) are the best states for the team k in the first and second sections, respectively. Also, \({P}_{1,min}^{k}\) and \({P}_{2,min}^{k}\) are the worst team k in the first and second parts. So, the following relationships are established:
$$\left\{\begin{array}{c}{P}_{1,max}^{k}=\left({W}_{1,max}^{k},{D}_{1,max}^{k},{L}_{1,max}^{k}\right) =(⌊\frac{k-1}{2}⌋, \left(k-1\right)\%2, ⌊\frac{k-1}{2}⌋\\ {P}_{1,min}^{k}=\left( {W}_{1,min}^{k},{ D}_{1,min}^{k},{ L}_{1,min}^{k}\right) =\left(0, 0, k-1\right) \\ \begin{array}{c}{P}_{2,max}^{k}=\left({W}_{2,max}^{k},{D}_{2,max}^{k},{L}_{2,max}^{k}\right) =\left(n-k, 0, 0\right) \\ {P}_{2,min}^{k}=\left({ W}_{2,min}^{k},{ D}_{2,min}^{k},{ L}_{2,min}^{k}\right) =\left(0, n-k, 0\right) \end{array}\end{array}\right.$$
12
Therefore, considering that team k has both the first part's characteristics and the second part's characteristics. We can consider the following relation:
$$\left\{\begin{array}{c}{P}_{max}^{k}={P}_{1,max}^{k}+{P}_{2,max}^{k}=(n-k+⌊\frac{k-1}{2}⌋, \left(k-1\right)\%2, ⌊\frac{k-1}{2}⌋)\\ {P}_{min}^{k}={P}_{1,min}^{k} +{ P}_{2,min}^{k}=\left(0, n-k, k-1\right) \end{array}\right.$$
13
The following relation can be obtained by considering relations 6 and 13:
$$n-k-1\le (3\times {W}_{k}+{D}_{k})\le 3\times \left(n-k+⌊\frac{k-1}{2}⌋\right)+ \left(k-1\right)\%2$$
14
Also, the following conditions can be considered in the production of possible states. Assuming (a, b, c) as the values of the (r-1)th row and (x, y, z) as the values of the rth row, we can use the following in the 'stageList' function.
We should correct Eq. 5 since (x, y, z) are the effects of the above row results.
$${P}_{r}\left(n\right)= {w}_{r}+{d}_{r}+{l}_{r}=n-\left(x+y+z\right)-1,$$
15
Values (W, D, L) must be selected from the following values:
$$\left\{\begin{array}{c}W\leftarrow \left[0 \cdots n-k-1\right] \\ D\leftarrow \left[0 \cdots n-W-y-1\right] \\ L\leftarrow [0 \cdots n-W-D-z-1]\end{array}\right.$$
16
Also, each row must have equal values or values less than the above row. Therefore, the following limitation must be added:
$$3\times \left(W+x\right)+D+y\le 3*a+b$$
17
According to the contents of this section, the stageList function can be developed as follows:
$$stageList :: Int \to Int \to Row \to Row \to \left[Row\right]$$
$$stageList n k \left(a,b,c\right) \left(x,y,z\right) = reverse\left[(w,d,l)\right| w\leftarrow \left[0..n-x-1\right], d\leftarrow \left[0..n-y-1\right], l\leftarrow \left[0..n-z-1\right],$$
$$w+d+l==n-\left(x+y+z\right)-1, 3*\left(w+x\right)+d+y<=3*a+b, 3* w+d>=n-k-1,$$
$$3*w+d<=(n-k+ div (k-1\left) 2\right)*3 + \left(mod \right(k-1\left) 2\right)]$$