English 中文(简体)
需要帮助利用C图表解决问题
原标题:Need help solving a problem using graphs in C

i m coding a c project for an algorithm class and i really need some help! Here s the problem:

I have a set of names like this one N = (James,John,Robert,Mary,Patricia,Linda Barbara) wich are stored in an RB tree. Starting from this set of names a series of couple like those ones are formed:

(James,Mary) (James,Patricia) (John,Linda) (John,Barbara) (Robert,Linda) (Robert,Barbara)

现在,需要以能够形成一小群子的方式合并这些要素,并限制每个奶制品都得到尊重,而该群体最弱的心脏。

With the couples in the example they will form two groups (James,Mary,Patricia) and (John,Robert,Barbara,Linda).

任务是将组建的团体的最高数目以及该团体中男女人数按最高标准计算。

In this case it would be 2 2 2

I was thinking about building a graph where every name is represented by a vertex and two vertex are in an edge only if they are paired. I can then use an algorithm (like Kruskal) to find the Minimum spanning tree.Is that right?

The problem is that the graph would not be completely connected. I also need to find a way to map the names to the edges of the Graph and vice-versa. Can the edges be indexed by a string?

Every help is really appreciated :) Thanks in advice!

问题回答

你们不需要找到最小的树木。 确实,这是为了在图表中找到“最佳”优势,该图将继续连接。 换言之,你不关心John和Robert之间的联系,而只是他们的联系。

你们说,问题在于,图表并非完全相连,但我认为这实际上已经到了。 如果按照你的建议使用夫妇来代表graph子,那么相关vert就形成了你所期望的群体。

例如,James与Mary有联系,James也与Patricia有联系。 没有任何其他人与这3个vert子(如果是的话,你会有另外的夫妇,包括他们),这就是为什么他们组成一个单一集团(James, Mary, Patricia)。 同样,John、Robert、Barbara和Linda都相互连接。

你的任务确实是编造图表,找到彼此脱节的所有相关图表。

While not a full algorithm, I hope that helps get you started.

I think that you can easily solve this with a dfs and connected components. Because every person(node) has a relation with an other one (edge). So you have an outer loop and run an explore function for every node which is unvisited and add the same number for every node explored by the explore function. e.g

dfs() {
int group 0;
for(int i=0;i<num_nodes;i++) {
if(nodes[i].visited==false){
explore(nodes[i],group);
group++;
}
}

那么,你简单地必须打上集团的 no子,然后,你准备就绪。 如果你想要跟踪你能够使用一个预数,表明第一、第二.点探点。

(对我的坏官办公室感到担忧)!

姓名和密码已经形成图表。 与其他节点的分点和分点数据结构只是另一个代表,而你不一定需要这种代表。 Disjoint set更容易执行海事组织,其生命目的恰恰是把同一事物的轨迹放在一起。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签