public SymbolGraph(T[,] inputs) { indexes = new LinearProbingHashSymbolTable <T, int>(); // 构建顶点到索引的映射 for (int i = 0; i < inputs.GetLength(0); i++) { for (int j = 0; j < inputs.GetLength(1); j++) { if (!indexes.Contains(inputs[i, j])) { indexes.Put(inputs[i, j], indexes.Size); } } } // 构建索引到顶点的映射 keys = new T[indexes.Size]; foreach (var item in indexes) { keys[indexes.Get(item.Key)] = item.Key; } // 构建图 graph = new UndirectedGraph(indexes.Size); for (int i = 0; i < inputs.GetLength(0); i++) { int v = indexes.Get(inputs[i, 0]); for (int j = 1; j < inputs.GetLength(1); j++) { graph.AddEdge(v, Index(inputs[i, j])); } } }
/// <summary> /// 判断图中是否包含顶点 /// </summary> /// <param name="vertex">顶点</param> public bool Contains(T vertex) { return(indexes.Contains(vertex)); }