I have a list of names, for example:
"Alice", "Bob", "Steve", "Kate", "Jane", "John", "Vic", "Dan", "Robert"
They call each other in a circle chain:
"Alice" -> "Bob" -> "Steve" -> "Kate" -> "Jane" -> "John" -> "Vic" -> "Dan" -> "Robert" -> "Alice"
I need an algorithm which will print several chains where in the result every person should have a call with all other persons without repeats. So if Vic called Dan and John called Vic yestarday all of them should have another persons in today call.
Your task can be achieved by the following MiniZinc model:
include "circuit.mzn";
int: n = 9; % number of participants
int: rounds = 4;
set of int: N = 1..n;
set of int: Rounds = 1..rounds;
% decision variables
array[N, Rounds] of var N: x;
predicate is_chain(array[N] of var N: c) =
circuit(c);
predicate is_talking_to(N: p1, N: p2, Rounds: r) =
(x[p1, r] == p2) \/
(x[p2, r] == p1);
constraint forall(r in Rounds) (circuit([x[p, r] | p in N]));
constraint forall(r1 in Rounds, r2 in r1+1..rounds, p1 in N, p2 in p1+1..n)
(is_talking_to(p1, p2, r1) -> not is_talking_to(p1, p2, r2));
function string: show_chain(array[N] of var N: c) =
join(" -> ", [ "\(c[p])" | p in N ]) ++ " -> \(c[1])\n";
output [ show_chain([fix(x[p, r]) | p in N]) | r in Rounds ];
Example output:
7 -> 5 -> 2 -> 9 -> 8 -> 4 -> 3 -> 6 -> 1 -> 7
4 -> 8 -> 9 -> 7 -> 1 -> 5 -> 2 -> 3 -> 6 -> 4
6 -> 9 -> 1 -> 3 -> 7 -> 2 -> 8 -> 4 -> 5 -> 6
2 -> 4 -> 6 -> 5 -> 3 -> 7 -> 9 -> 1 -> 8 -> 2
For more than four rounds, no solution was found.
The model interpretes to "have a call" as symmetric "speaking to eachother". It does not matter, who is initiating a call.
Imagine we have a Context Free Grammer, CFG, as follows:
S -> a ...(1)
S -> SS ...(2)
And i derive a string in the specified language as follows:
S ..start state
SS ..using (2)
aS ...using (1) <- is this valid, like only applying the subsititution rule on 1 variable instead of all same variables?
So my question is if i were to apply rule (1) on "SS", do i have the option to apply (1) to only 1 S of the "SS" or do i have to apply to both of them?
You can apply the rule to only one S, or as many as you like. Here is a slightly more complicated example that maybe better illustrates the idea:
S -> a (1)
S -> b (2)
S -> SS (3)
So, what strings are in this language? Here are the first few strings with derivations:
a: S -> a
b: S -> b
aa: S -> SS -> aS -> aa
S -> SS -> Sa -> aa
ab: S -> SS -> aS -> ab
S -> SS -> Sb -> as
ba: S -> SS -> bS -> ba
S -> SS -> Sa -> ba
bb: S -> SS -> bS -> bb
S -> SS -> Sb -> bb
aaa: S -> SS -> aS -> aSS -> aaS -> aaa
S -> SS -> aS -> aSS -> aSa -> aaa
S -> SS -> Sa -> SSa -> aSa -> aaa
S -> SS -> Sa -> SSa -> Saa -> aaa
aab
...
Anyway, we find that the grammar generates all non-empty strings of a's and b's, including those with mixed up a's and b's. If we had to replace all S's with the same rule, we would get a much, much smaller language, if we'd get a (non-empty) language at all.
I have a data structure like this:
Map(
0 -> Array((1,1), (2,1)),
1 -> Array((2,1), (3,1), (0,1)),
2 -> Array((4,1), (0,1), (1,1)),
3 -> Array((1,1)),
4 -> Array((5,1), (2,1)),
5 -> Array((4,1))
)
and the result I need should look like this:
Map(
0 -> Map(1 -> 1, 2 -> 1),
1 -> Map(2 -> 1, 3 -> 1, 0 -> 1),
2 -> Map(4 -> 1, 0 -> 1, 1 -> 1),
3 -> Map(1 -> 1),
4 -> Map(5 -> 1, 2 -> 1),
5 -> Map(4 -> 1)
)
It is really simple: yourMap.mapValues(_.toMap)
scala> :paste
// Entering paste mode (ctrl-D to finish)
Map(
0 -> Array((1,1), (2,1)),
1 -> Array((2,1), (3,1), (0,1)),
2 -> Array((4,1), (0,1), (1,1)),
3 -> Array((1,1)),
4 -> Array((5,1), (2,1)),
5 -> Array((4,1))
)
// Exiting paste mode, now interpreting.
res0: scala.collection.immutable.Map[Int,Array[(Int, Int)]] = Map(0 -> Array((1,1), (2,1)), 5 -> Array((4,1)), 1 -> Array((2,1), (3,1), (0,1)), 2 -> Array((4,1), (0,1), (1,1)), 3 -> Array((1,1)), 4 -> Array((5,1), (2,1)))
scala> res0.mapValues(_.toMap)
res1: scala.collection.immutable.Map[Int,scala.collection.immutable.Map[Int,Int]] = Map(0 -> Map(1 -> 1, 2 -> 1), 5 -> Map(4 -> 1), 1 -> Map(2 -> 1, 3 -> 1, 0 -> 1), 2 -> Map(4 -> 1, 0 -> 1, 1 -> 1), 3 -> Map(1 -> 1), 4 -> Map(5 -> 1, 2 -> 1))
Let's say there is a grammar
S -> PQT
R -> T
U -> aU | bX
X -> Y
P -> bQ
Y -> SX | c | X
Q -> aRY
T -> U
There is a loop:
X -> Y
Y -> X
How to eliminate it when converting to CNF?
I don't think it's fine to add a rule to grammar (as in unit elimination)
X -> X, right, because it s basically another loop?
If X -> Y and Y -> X, the nonterminal symbols are interchangeable and you can safely replace all instances of either of the two with the other of the two, eliminating one of the two completely. As you also pointed out, rules of the form X -> X can be safely eliminated. So this grammar is equivalent to the one you give:
S -> PQT
R -> T
U -> aU | bX
P -> bQ
X -> SX | c
Q -> aRX
T -> U
And so is this one:
S -> PQT
R -> T
U -> aU | bY
P -> bQ
Y -> SY | c
Q -> aRY
T -> U
I want to find the minimal cover for the following set of functional dependencies:
A -> BC
B -> C
A -> B
AB -> C
first step: Break down the RHS of each functional dependency into a single attribute:
A -> B
A -> C
B -> C
A -> B
AB -> C
then I will remove of the two A -> B, so we will get:
A -> B
A -> C
B -> C
AB -> C
second step: Trying to remove unnecessary attributes from the LHS of each functional dependency (whose LHS has 2 or more attributes):
for AB -> C , check if A is necessary by:
replace AB -> C with B -> C so if B+ contains C then A is unnecessary:
B+ = B (B -> C)
= BC (so A is unnecessary)
check if B is necessary by:
replace AB -> C with A -> C so if A+ contains C then B is unnecessary:
A+ = A (A -> B)
= AB (A -> C)
= ABC (so B is unnecessary)
now we have:
A -> B
A -> C
B -> C
third step: Trying to remove unecessary functional dependencies:
for A -> B check if A+ contains B without using A -> B:
A+ = A (A -> C)
= AC (so A -> B is necessary)
for A -> C check if A+ contains C without using A -> C:
A+ = A (A -> B)
= AB (so A -> C is necessary)
for B -> C check if B+ contains C without using B -> C:
B+ = B (so B -> C is necessary)
now we have:
A -> B
A -> C
B -> C
Finally, group the functional dependencies that have common LHS together:
A -> BC
B -> C
so we can say that these functional dependencies are the minimal cover of the set, is that true ? and how we can deduce the key(s) of the set?
To compute a canonical cover for F:
Use the union rule to replace any dependencies with common left-side.
so Combine A ->BC and A -> B into A -> BC
Set is now {A -> BC, B -> C, AB -> C}
A is extraneous in AB -> C
Check if the result of deleting A from AB -> C is implied by the other dependencies
Yes: in fact, B -> C is already present!
Set is now {A -> BC, B -> C}
C is extraneous in A -> BC
Check if A -> C is logically implied by A B and the other dependencies
Yes: using transitivity on A -> B and B -> C.
Can use attribute closure of A in more complex cases
The canonical cover is: A ->B, B -> C
source:Korth,sudarshan DBMS book.