CSP: Criminal Search and Prosecution Problem [closed] - artificial-intelligence

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I'm doing some homework about Artificial Intelligence, and now I'm stuck in this question:
You are a detective in charge of bringing down drug dealers (D). A tip has led you to a small apartment complex where you believe one or more D might be hiding out. There are five apartments in a row. Each apartment could contain a drug dealer D or could contain innocent people: adults (A), families with babies (B), or with teenagers (T). Before you break down a door, you need to be absolutely sure that a dealer D is inside, otherwise you could get sued for police suboptimality.
To help you narrow down where drug dealers D might be (if any are there at all!), you use the fact that different people make different noises. Every time you walk between two apartments, you can hear the louder of the two noises that are being made in those apartments. The loudest people are teenagers T, who blast music (m), the next loudest are babies B who cry (c), the next loudest is the drug dealer D, who makes a rustling sound (r), and the quietest people are adults A, who are totally silent (s). For example, if there were a baby in one house and a teenager in next, you would hear music (m) when standing between those apartments. Walking by the five apartments, you hear the noises shown in the diagram above. You decide to try solving this problem as a CSP
The question made me confused is: List all solutions to this CSP or state that none exist.
I assumed that my answer is correct, which is:
1T
2B
3A
4A
5T
but the correct answer I found is:
1T
2B
3D
4A
5T
and
1T
2B
3D
4D
5T
and
1T
2B
3A
4D
5T
I think maybe I didn't get the question correctly. So anyone can explain this to me, which one is correct?

The abstract way to look at this is to see what can you eliminate from each house. You know you can hear the loudest noise between houses which means one or both of the houses on either side of the noise could contain that noise maker. So in paragraph form:
Between 1 & 2 is music so 1 and 2 might each contain a Teen and that is all we know.
Between 2 & 3 is crying so we know 2 does not contain anything louder than crying (music) so 2 cannot contain a T, therefore 1 must contain the Teen.
Between 3 & 4 is rustling so we know 3 or 4 might contain a Dealer. We also now know nothing louder than rustling exists in 3, therefore 2 must contain a Baby.
Between 4 & 5 is music so now we have a problem. We know 5 must contain a Teen, otherwise music would have been louder than rustling and we would not have heard rustling between 3 & 4. However, now all we know about 3 & 4 is that they might contain anything as quiet or quieter than a Dealer, and also that at least one must contain a dealer. So those combinations are [A, D], [D, D], [D, A].
That gives us a set of final answers [T, B, A, D, T] or [T, B, D, D, T] or [T, B, D, A, T]

Related

Six degree of separation interview problem

A was asked an interesting question on an interview lately.
You have 1 million users
Each user has 1 thousand friends
Your system should efficiently answer on Do I know him? question for each couple of users. A user "knows" another one, if they are connected through 6 levels of friends.
E.g. A is friend of B, B is a friend of C, C is friend of D, D is a friend of E, E is a friend of F. So we can say that, A knows F.
Obviously you can't to solve this problem efficiently using BFS or other standard traversing technic. The question is - how to store this data structure in DB and how to quickly perform this search.
What's wrong with BFS?
Execute three steps of BFS from the first node, marking accessible users by flag 1. It requires 10^9 steps.
Execute three steps of BFS from the second node, marking accessible users by flag 2. If we meet mark 1 - bingo.
What about storing the data as 1 million x 1 million matrix A where A[i][j] is the minimum number of steps to reach from user i to user j. Then you can query it almost instantly. The update however is more costly.

Algorithm for evenly spacing list items (playlist songs) along several categories (id3 tags)

I am having trouble designing an algorithm to assist in the creation of an mp3 playlist, although an algorithm for the more general case of evenly spacing items in a list could be adapted for my use.
The general case is that I would like to reorder items in a list to maximize their diversity along several axes.
My specific use-case is that I want to dump a bunch of songs into a collection, then run my algorithm over the collection to generate an ordered playlist. I would like the order to follow this set of criteria:
maximize the distance between instances of the same artist
maximize the distance between instances of the same genre
maximize the distance between instances of category X
etc for N categories
Obviously we could not guarantee to optimize ALL categories equally, so the first category would be weighted most important, second weighted less, etc. I definitely want to satisfy the first two criteria, but making the algorithm extensible to satisfy N would be fantastic. Maximizing randomness (shuffle) is not a priority. I just want to diversify the listening experience no matter where I come in on the playlist.
This seems close to the problem described and solved here, but I can't wrap my head around how to apply this when all of the items are in the same list with multiple dimensions, rather than separate lists of differing sizes.
This seems like a problem that would have been solved many times by now but I am not able to find any examples of it.
This should be much faster than brute-force:
Order all the songs randomly.
Compute the weights for each song slot (i.e. how close is it to the same artist/genre/etc.). It will be a number from 1-N indicating how may songs away it is from a match. Lower is worse.
Take the song with the lowest weight, and swap that song with a random other song.
Re-compute the weights of the swapped songs. If either got worse, reverse the swap and go back to 3.
For debugging, print the "lowest weight" and overall average weight. (debugging)
Go to 2
You won't find the optimal this way, but it should give mediocre results pretty fast, and eventually improve.
Step 2 can be made fast this way: (pseudo code in Ruby)
# Find the closest match to a song in slot_number
def closest_match(slot_number)
# Note: MAX can be less than N. Maybe nobody cares about songs more than 20 steps away.
(1..MAX).each |step|
return step if matches?(slot_number+step, slot_number) or matches?(slot_number-step, slot_number)
end
return MAX
end
# Given 2 slots, do the songs there match?
# Handles out-of-bounds
def matches?(x,y)
return false if y > N or y < 1
return false if x > N or x < 1
s1 = song_at(x)
s2 = song_at(y)
return true if s1.artist == s2.artist or s1.genre == s2.genere
return false
end
You also don't have to re-compute the whole array: If you cache the weights, you only need to recompute songs that have weight >=X if they are X steps away from a swapped song. Example:
| Song1 | Song2 | Song3 | Song4 | Song5 |
| Weight=3 | Weight=1 | Weight=5 | Weight=3 | Weight=2|
If you are swapping Song 2, you don't have to re-compute song 5: It's 3 steps away from Song 3, but it's weight was 2, so it won't "see" Song 3.
Your problem is probably NP-hard. To get a sense of it, here's a reduction to CLIQUE (an NP-hard problem). That doesn't prove that your problem is NP-hard, but at least gives an idea that there is a connection between the two problems. (To show definitively that your problem is NP-hard, you need a reduction in the other direction: show that CLIQUE can be reduced to your problem. I feel that it is possible, but getting the details right is fussy.)
Suppose you have n=6 songs, A, B, C, D, E, and F. Lay them out in a chart like this:
1 2 3 4 5 6
A A A A A A
B B B B B B
C C C C C C
D D D D D D
E E E E E E
F F F F F F
Connect each item in column 1 with an edge to every other item in every other column, except for items in the same row. So A in column 1 is connected to B, C, D, E, F in column 2, B, C, D, E, F in column 3, and so on. There are n^2 = 36 nodes in the graph and n*(n-1)^2 + n*(n-1)*(n-2) + n*(n-1)*(n-3) + ... = n*(n-1)*n*(n-1)/2 = O(n^4) edges in the graph.
A playlist is a maximum clique in this graph, in other words a selection which is mutually consistent (no song is played twice). So far, not so hard: it's possible to find many maximum cliques very quickly (just permutations of the songs).
Now we add information about the similarity of the songs as edge weights. Two songs that are similar and close get a low edge weight. Two songs that are similar and far apart get a higher edge weight. Now the problem is to find a maximum clique with maximum total edge weight, in other words the NP-hard problem CLIQUE.
There are some algorithms for attacking CLIQUE, but of course they're exponential in time. The best you're going to be able to do in a reasonable amount of time is either to run one of those algorithms and take the best result it can generate in that time, or to randomly generate permutations for a given amount of time and pick the one with the highest score. You might be able to get better results for natural data using something like simulated annealing to solve the optimization problem, but CLIQUE is "hard to approximate" so I have the feeling you won't get much better results that way than by randomly generating proposals and picking the highest scoring.
Here is my idea: you create a graph, where songs are vertices, and paths represent their diversity.
For example we have five songs:
"A", country, authored by John Doe
"B", country, authored by Jane Dean
"C", techno, authored by Stan Chang
"D", techno, authored by John Doe
"E", country, authored by John Doe
We assign weight 2 to artist and 1 to genre, and use multiplicative inverse as path's value. Some of the path will look like this:
A-B: 2*1 + 1*0 = 2 => value of the path is 1/2 = 0.5
A-C: 2*1 + 1*1 = 3 => value of the path is 1/3 = 0.33
A-D: 2*0 + 1*1 = 1 => value of the path is 1/1 = 1
A-E: 2*0 + 1*0 = 0 => value of the path is 1/0 = MAX_DOUBLE
You can have as many categories as you want, weighted as you wish.
Once you have calculated all paths between all songs, all you have to do is use some heuristic algorithm for Travelling Salesman Problem.
EDIT:
I'd like to throw another constraint on the problem: the "maximal distance" should take into account the fact that the playlist may be on repeat. This means that simply putting two songs by the same artist at opposite ends of the playlist will fail since they will be "next to" each other when the list repeats.
Part of Travelling Salesman Problem is that in the end you return to your origin point, so you will have the same song at both ends of your playlist, and both paths (from song and to song) will be calculated with the best efficiency allowed by used heuristics. So all you have to do is remove last entry from your result (because it's the same as the first one) and you can safely repeat without breaking your requirements.
A brute force algorithm for that is easy.
maxDistance = 0
foreach ordering
distance = 0
foreach category
for i=1 to numsongs
for j=i+1 to numsongs
if song i and song j in this ordering have same value for this category
distance = distance + (j-i)*weight_for_this_category
endif
endfor
endfor
endfor
if ( distance > maxDistance )
maxDistance = distance
mark ordering as best one so far
endif
endfor
But that algorithm has worse than exponential complexity with the number of songs so it will take unmanageable ammounts of time pretty fast. The hard part comes in doing it in a reasonable time.
I was thinking about a "spring" approach. If new items are added to the end of the list, they squish the similar items forward.
If I add pink Floyd to the list, then all other Floyd songs get squished to make space.
I would implement the least common dimensions before the most common dimensions, to ensure the more common dimensions are better managed.
For tags in song ordered by count tags in list asc
Evenly space earlier songs with knowledge new song being added
Add song

Cannibals and Missionaries with Strength [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need some help with a math task our professor gave us. Any suggestions would help.
The problem is:
There are N cannibals and M missinaries. All missionaries have a strenght attribute, which can be 1 or any positive whole number. Strenght indicates how many cannibals he can fight off.
Basically: there are two sides of the river, there is a 2-slot boat, and you have to transfer all the guys to the other side, without letting the cannibals eat the missionaries.
How would you write a program for this? What would be the transferring-grouping algorythm?
Thanks in anticipation,
Mark.
Model your problem as a states graph.
In here, a state is ({L,R}n,{L,R}m,{L,R}) Where:
First n entries: one for each missionary - where he is: left/right bank of the river
next,m entries: one for each canibal- where he is: left/right bank of the river
Last entry is for the boat
These are your vertices - you should also trim the invalid states - where strength of missionaries is not enough in one (or more) side. It is easy to calculate it for each state.
Your edges are:
E = { (S1,S2) | Can move in one boat ride from S1 to S2 }
All is left to do - use some shortest path algorithm to find the shortest path from: (L,L,....,L) to (R,R,...,R).
You can use BFS for this task, or even bi-directional search - or an informed algorithm (with admissible heuristic) such as A* Algorithm.
PS. The 'graph' is just conceptual, in practice you will have a function next:S->2^S, that given a state - returns all valid successors of this state (states that you can get to them using one edge on the graph from S). This will allow you to "generate the graph" on the fly.
Your next(S) function should be something like (high level pseudo code, without optimizations):
next(S):
let x be the bank where the boat is, and y the other bank
for each person p1 on bank x:
S' = S where boat and p1 moved from x to y
if S' is valid according to strength limitations, yield S'
for each p2 != p1 on bank x:
S' = S where boat and p1 and p2 moved from x to y
if S' is valid according to strength limitations, yield S'

Does the position of the blank in an n-puzzle solution affect the set of valid puzzles?

I'm having trouble with my n-puzzle solver. Thought it was working, but it turns out it is solving insoluble puzzles. I've tried to trace it, but that's a lot of tracing and so far I see no cheating. I think I understand the algorithm for determining solubility, and my implementation agrees with the odd/even parity of some examples from the web... that is to say, when I count up the number of tiles after a given tile that are smaller than it, for every tile, and then add the row index of the blank tile, I get the same odd or even number as others have gotten.
So a thought that has occurred to me. In my model of, say, the 8-puzzle, my solution state is:
_ 1 2
3 4 5
6 7 8
Rather than
1 2 3
8 _ 4
7 6 5
Or
1 2 3
4 5 6
7 8 _
As it is in some other formulations. Could this be affecting which puzzles are soluble and which are not?
Thanks!
z.
In general, yes: If a configuration is solvable to the standard solution, it will not be solvable to an unsolvable configuration.
In particular, it depends on the exact configuration you're using as a solution. You will need to check to see if you can solve from that configuration to the standard one.
EDIT: This of it this way:
Let A be the standard solution.
Let B be your preferred solution.
Let C be your starting configuration.
If you can get from A to B, and you can get from C to A, then you can get from C to B.
But if you can't get from A to B, and you can get from C to A, then you can't get from C to B.

Database Relational Homework help

The Problem "Consider a relation R with five attributes ABCDE. You are given the following dependancies
A->B
BC->E
ED->A
List all the keys for R.
The teacher gave us the keys, Which are ACD,BCD,CDE
And we need to show the work to get to them.
The First two I solved.
For BCD, the transitive of 2 with 3 to get (BC->E)D->A => BCD->A.
and for ACD id the the transitive of 1 with 4 (BCD), to get (A->B)CD->A => ACD->A
But I can't figure out how to get CDE.
So it seems I did it wrong, after googling I found this answer
methodology to find keys:
consider attribute sets α containing: a. the determinant attributes of F (i.e. A, BC,
ED) and b. the attributes NOT contained in the determined ones (i.e. C,D). Then
do the attribute closure algorithm:
if α+ superset R then α -> R
Three keys: CDE, ACD, BCD
Source
From what I can tell, since C,D are not on the left side of the dependencies. The keys are left sides with CD pre-appended to them. Can anyone explain this to me in better detail as to why?
To get they keys, you start with one of the dependencies and using inference to extend the set.
Let me have a go with simple English, you can find formal definition the net easily.
e.g. start with 3).
ED -> A
(knowing E and D, I know A)
ED ->AB
(knowing E and D, I know A, by knowing A, I know B as well)
ED->AB
Still, C cannot be known, and I have used all the rules now except BC->E,
So I add C to the left hand side, i.e.
CDE ->AB
so, by knowing C,D and E, you will know A and B as well,
Hence CDE is a key for your relation ABCDE. You repeat the same process, starting with other rules until exhausted.

Resources