C# 如何计算两个节点之间的关系强度?
所以我尝试制作一个类似于凯文·培根六度游戏的应用程序。首先我从一个文本文件中读到,其中包含演员和他们主演的电影。它的格式是这样的: 因此,我将我的图形存储为一个邻接列表,该列表将演员作为节点和他们的邻居,以及他们主演了哪些电影(即DictionaryC# 如何计算两个节点之间的关系强度?,c#,dictionary,graph,breadth-first-search,C#,Dictionary,Graph,Breadth First Search,所以我尝试制作一个类似于凯文·培根六度游戏的应用程序。首先我从一个文本文件中读到,其中包含演员和他们主演的电影。它的格式是这样的: 因此,我将我的图形存储为一个邻接列表,该列表将演员作为节点和他们的邻居,以及他们主演了哪些电影(即Dictionary)(即演员A:Actor B movie 0,movie 1,movie 7,Actor C movie 8 Actor Z movie 9…等等)我知道数据结构很奇怪,但我被要求这样做。这是我的图形课 class Graph { publi
class Graph
{
public Dictionary<string, HashSet<Dictionary<string, HashSet<string>>>> dic;
public Dictionary<string, HashSet<string>> dic1;
int count = 0;
public Graph()
{
dic = new Dictionary<string, HashSet<Dictionary<string, HashSet<string>>>>();
//dic2 = new Dictionary<string, Dictionary<string, int>>();
}
public Graph(string filename, string delimeter)
{
dic = new Dictionary<string, HashSet<Dictionary<string, HashSet<string>>>>();
//dic2 = new Dictionary<string, Dictionary<string, int>>();
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() != -1)
{
string line = sr.ReadLine();
char r;
r = Convert.ToChar(delimeter);
int j;
int i=0;
string[] names = line.Split(r);
for (j=1; j < names.Length; j++)
{
for (i = j; i < names.Length; i++)
{
if (!dic.ContainsKey(names[i])) //without this line code throws exception key is not found
addVertex(names[i]);
if (!(dic[names[i]].Equals(dic[names[j]])))
addEdge(names[i], names[j], names[0]);
}
}
}
sr.Close();
}
public void addEdge(string a, string b, string c)
{
if (!dic.ContainsKey(a))
{
addVertex(a);
}
if (!dic.ContainsKey(b))
{
addVertex(b);
}
dic1 = new Dictionary<string, HashSet<string>>();
dic1.Add(a, new HashSet<string>());
dic1.Add(b, new HashSet<string>());
dic1[b].Add(c);
dic1[a].Add(c);
dic[a].Add(dic1);
dic[b].Add(dic1);
}
public void addVertex(string v)
{
if (!dic.ContainsKey(v))
dic.Add(v, new HashSet<Dictionary<string, HashSet<string>>>());
}
public IEnumerable<Dictionary<string, HashSet<string>>> adjacentto(string v)
{
return dic[v];
}
}
类图
{
公共词典;
公共词典dic1;
整数计数=0;
公共图()
{
dic=新字典();
//dic2=新字典();
}
公共图(字符串文件名、字符串delimeter)
{
dic=新字典();
//dic2=新字典();
FileStream fs=newfilestream(文件名,FileMode.Open);
StreamReader sr=新的StreamReader(fs);
while(sr.Peek()!=-1)
{
字符串行=sr.ReadLine();
字符r;
r=换算成焦耳(delimeter);
int j;
int i=0;
string[]name=line.Split(r);
对于(j=1;j
然后我要计算我应用BFS算法的每两个节点之间的分离度。这是我的BFS课程:
class BFS
{
private Dictionary<string, int> dist = new Dictionary<string, int>();
//private Dictionary<string, HashSet<int>> prev = new Dictionary<string, HashSet<int>>();
public void BFSDegree(Graph g, string s, string p)
{
Queue<string> q = new Queue<string>();
dist.Add(s, 0);
q.Enqueue(s);
//int count;
//int v = 0;
while (q.Count() != 0)
{
string j = q.Dequeue();
//count = 0;
foreach (Dictionary<string, HashSet<string>> h in g.adjacentto(j))
{
foreach (string m in h.Keys)
{
if (!dist.ContainsKey(m))
{
q.Enqueue(m);
dist.Add(m, 1 + dist[j]);
}
if (j == p)
{
Console.WriteLine(" " + dist[j]);
//count++;
return;
}
}
}
}
}
public void BfsOnFile(string filename, string delimeter, Graph G)
{
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string actor1 = "";
string actor2 = "";
while (sr.Peek() != -1)
{
string line = sr.ReadLine();
char r;
r = Convert.ToChar(delimeter);
string[] names = line.Split(r);
for (int i = 0; i < names.Length; i++)
{
actor1 = names[0];
actor2 = names[1];
}
Console.Write(" " + line);
BFSDegree(G, actor1, actor2);
dist.Clear();
}
}
}
BFS类
{
私有字典dist=新字典();
//private Dictionary prev=新字典();
公共void BFSDegree(图g、字符串s、字符串p)
{
队列q=新队列();
添加区(s,0);
q、 排队;;
//整数计数;
//int v=0;
而(q.Count()!=0)
{
字符串j=q.Dequeue();
//计数=0;
foreach(字典h在g.邻接(j))
{
foreach(h键中的字符串m)
{
如果(!dist.ContainsKey(m))
{
q、 排队(m);
dist.Add(m,1+dist[j]);
}
如果(j==p)
{
Console.WriteLine(“+dist[j]);
//计数++;
返回;
}
}
}
}
}
公共void BfsOnFile(字符串文件名、字符串delimeter、图G)
{
FileStream fs=newfilestream(文件名,FileMode.Open);
StreamReader sr=新的StreamReader(fs);
字符串actor1=“”;
字符串actor2=“”;
while(sr.Peek()!=-1)
{
字符串行=sr.ReadLine();
字符r;
r=换算成焦耳(delimeter);
string[]name=line.Split(r);
for(int i=0;i
通过这两门课,我可以计算出它们之间的分离程度。现在我的问题是计算两个节点之间的关系强度。例如,作用子A和作用子B以等于1的分离度直接连接。他们之间的关系强度是三部电影,因为他们直接出现在三部电影中。
参与者C和参与者E通过参与者a、参与者B或参与者D以等于2的分离度连接。关系强度应等于三个选项中的最大值。因此,根据我的理解,如果我要计算演员A和演员B之间的关系强度,因为他们一起主演了3部电影,我应该去包含键B字典的键A字典,并计算其哈希集中的电影数量。但我不知道怎么做?喜欢的数据结构太复杂,无法处理,但不幸的是,我被要求这样做