简体   繁体   English

Java上的图形/数据结构算法

[英]Algorithm for Graph/Data Structure on Java

I have been working on the following problem where, I have a CSV file with two columns, we can say the filed names are "Friends". 我一直在研究以下问题,我有一个包含两列的CSV文件,我们可以说文件名是“朋友”。 Both the columns contain letters from A to Z. eg 两列都包含从A到Z的字母。例如

A B
B C
A E
D F
E F

Each row has two different letters(no duplication in the row). 每行有两个不同的字母(行中没有重复)。 A is a friend of B, C is a friend of D etc...If person A talks to person B and Person B talks to person C, then B and C will become aquitances. A是B的朋友,C是D等的朋友......如果A人与B人交谈,B人与C人交谈,则B和C将成为水上运动。 Aquintaces are who share a common friend. Aquintaces是一个共同的朋友。 I need to fin out who has more aquintances? 我需要找出谁有更多的饮料?

I have been trying with two different methods one using differnt data structures like hashmap, arraylist, stack etc, and another using graph theory (JGraphT library). 我一直尝试使用两种不同的方法,一种使用不同的数据结构,如hashmap,arraylist,stack等,另一种使用图论(JGraphT库)。 But, i am stuck with the logic if I use data strcutres and I am stuck with traversal in the graph if I use graph theory. 但是,如果我使用数据strcutres,我会坚持逻辑,如果我使用图论,我会在图中遇到遍历。

I have following questions:- 我有以下问题: -

  1. What is a better approach to go with data structures or graph? 使用数据结构或图形的更好方法是什么? Or any other better approach/logic/algorithm than this? 或者比这更好的方法/逻辑/算法?
  2. Does anyone know how to traverse a graph in JgraphT Library. 有谁知道如何遍历JgraphT库中的图形。 I am not able to do this, they have very limited documentation about the library. 我无法做到这一点,他们关于图书馆的文件非常有限。

Please, any help would really be appreciated. 请,任何帮助真的很感激。

Generally HashMaps are among the most rapid and easy to use. 通常,HashMaps是最快速且易于使用的。 I would recommend you use them rather any custom libraries, except if you are sure that some library will do easily something you need and something that will take longtime for you to code. 我建议您使用它们而不是任何自定义库,除非您确定某些库可以轻松完成您需要的东西以及需要很长时间才能编写代码的东西。

In your case, just you can just use each person as a key and the list of his friends as the object pointed to by. 在你的情况下,你可以只使用每个人作为一个键,并将他的朋友列表作为指向的对象。 Parsing your .csv file and filling the HashMap accordingly will solve your issue, as a previous comment pointed out. 正如之前的评论指出的那样,解析.csv文件并相应地填充HashMap将解决您的问题。

You can have a hash table first that maps every letter to the set of its friends, eg A maps to { B }, B maps to { C }, and if Z has two friends Y and W then Z maps to { Y, W }. 您可以先将哈希表映射到其朋友集,例如A映射到{B},B映射到{C},如果Z有两个朋友Y和W,则Z映射到{Y,W }。 This is a hash map from letters to sets-of-letters. 这是从字母到字母集的哈希映射。

To calculate the acquaintances, iterate through the hash map. 要计算熟人,请遍历哈希映射。 When you are at entry A -> { B, C, F } then iterate in an inner loop through the set B, C, F. Collect their friends (three sets) into a single set (just insert the elements into a temp set) and remove A from that set if A was found. 当您在条目A - > {B,C,F}时,然后在内部循环中迭代通过集合B,C,F。将他们的朋友(三组)收集到一个集合中(只需将元素插入到临时集中)如果找到A,则从该集中删除A. The size of that set is then the number of acquaintances for A. Rinse and repeat. 然后,该组的大小是A的熟人数。冲洗并重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM