#### Archived

This topic is now archived and is closed to further replies.

# Animal behaviour

This topic is 5406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a group of animals. Each has saved which of the other animals he likes to be in company with. Now I want to calculate a group in which all animals like eachother. I just cant seem to come up with a descent algorithm. Any ideas pointers? -CProgrammer

##### Share on other sites
You''re searching for cycles in a directed graph.

##### Share on other sites
Does somebody perhaps have a small code example in C/C++
for specifieing cycles in a directed graph?
-CProgrammer

##### Share on other sites
Wait a sec. If I find out the cycle in a directed graph would that really be correct?
Lets say this example:
We have Animal A,B,C,D,E,F

A->B->C->D->A
is a cycle.
Then it isnt said that C likes A for instance.
-CProgrammer

##### Share on other sites
To pick up your ABCDEF example...

Take animal A. Check which animals it likes to be with, animals B, D, E and F for instance. You can drop animal C already, as there will be at least one animal that doesn''t like C. So you''ll be left with animals A, B, D, E and F. Now go to the next animal (B) and check which animals in the group it doesn''t like. Eliminate those and go to the next one.

This method has its weaknesses, but it''s a very easy solution. The problem is, that if B doesn''t like E, but the other ones like E, if B gets thrown out by animal C, E isn''t in the list even though no animal is left in the group that doesn''t like E. You can probably work around this somehow by keeping another list of animals that have been thrown out and checking that list when you''ve assembled your basic group.

- Christoph

---
Teamwork Software - Stuff That Does Something

##### Share on other sites
quote:
Original post by Captain Nuss
To pick up your ABCDEF example...

Take animal A. Check which animals it likes to be with, animals B, D, E and F for instance. You can drop animal C already, as there will be at least one animal that doesn''t like C. So you''ll be left with animals A, B, D, E and F. Now go to the next animal (B) and check which animals in the group it doesn''t like. Eliminate those and go to the next one.

This method has its weaknesses, but it''s a very easy solution. The problem is, that if B doesn''t like E, but the other ones like E, if B gets thrown out by animal C, E isn''t in the list even though no animal is left in the group that doesn''t like E. You can probably work around this somehow by keeping another list of animals that have been thrown out and checking that list when you''ve assembled your basic group.

- Christoph

---
Teamwork Software - Stuff That Does Something

Thats a way to go yeah, although that problem may get tricky and CPU consuming, but i''ll think it through thanks.
-CProgrammer

one word:
boost::graph

##### Share on other sites
Hey thanks guys. I think I found an answer now. Kinda a mix of everything.
-CProgrammer

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633272
• Total Posts
3011166
• ### Who's Online (See full list)

There are no registered users currently online

×