public static GWGraph <NodeData, EdgeData, GraphData> BinaryTree <NodeData, EdgeData, GraphData>(int depth) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("BinaryTree (" + depth + ")"); var center = graph.CreateNode(); var currenNodes = new List <GWNode <NodeData, EdgeData, GraphData> >(); currenNodes.Add(center); var nextNodes = new List <GWNode <NodeData, EdgeData, GraphData> >(); for (int i = 0; i < depth; i++) { foreach (var parent in currenNodes) { var node1 = (graph.CreateNode()); var node2 = (graph.CreateNode()); nextNodes.AddRange(node1, node2); //if (rdm.NextDouble() > 0.9) graph.CreateEdge(parent, node1); graph.CreateEdge(parent, node2); } currenNodes = nextNodes; nextNodes = new List <GWNode <NodeData, EdgeData, GraphData> >(); } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> RandomTree <NodeData, EdgeData, GraphData>(int depth, int maxBranches) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("RandomTree (" + depth + "," + maxBranches + ")"); var center = graph.CreateNode(); var currenNodes = new List <GWNode <NodeData, EdgeData, GraphData> >(); currenNodes.Add(center); var nextNodes = new List <GWNode <NodeData, EdgeData, GraphData> >(); for (int i = 0; i < depth; i++) { foreach (var parent in currenNodes) { var children = rdm.Next(5); for (int child = 0; child < children; child++) { var node = (graph.CreateNode()); nextNodes.Add(node); graph.CreateEdge(parent, node); } } currenNodes = nextNodes; nextNodes = new List <GWNode <NodeData, EdgeData, GraphData> >(); } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> OneCoreElement <NodeData, EdgeData, GraphData>(int numberNodes) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("OneCoreElement (" + numberNodes + ")"); for (int i = 0; i < numberNodes; i++) { var node = graph.CreateNode(); } var nodes = graph.Nodes.ToList(); for (int i = 0; i < numberNodes; i++) { //for (int k = i; k < numberNodes; k++) { //if (rdm.NextDouble() > 0.9) graph.CreateEdge(nodes[i], nodes[rdm.Next(nodes.Count)]); if (i < 1) { for (int k = 0; k < 10; k++) { graph.CreateEdge(nodes[i], nodes[rdm.Next(nodes.Count)]); } } } } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> Clone <NodeData, EdgeData, GraphData>(this IGWGraph <NodeData, EdgeData, GraphData> originalGraph, Func <IGWNode <NodeData, EdgeData, GraphData>, NodeData> nodeDataConverter, Func <IGWEdge <NodeData, EdgeData, GraphData>, EdgeData> edgeDataConverter, Func <IGWGraph <NodeData, EdgeData, GraphData>, GraphData> graphDataConverter) { var newGraph = new GWGraph <NodeData, EdgeData, GraphData>(); newGraph.Data = graphDataConverter(originalGraph); var nodesDict = new Dictionary <IGWNode <NodeData, EdgeData, GraphData>, GWNode <NodeData, EdgeData, GraphData> >(); foreach (var node in originalGraph.Nodes) { var newNode = newGraph.CreateNode(); nodesDict.Add(node, newNode); newNode.Data = nodeDataConverter(node); newNode.GraphId = node.GraphId; } foreach (var edge in originalGraph.Edges) { var newEdge = newGraph.CreateEdge(nodesDict[edge.Foot], nodesDict[edge.Head]); newEdge.Data = edgeDataConverter(edge); } return(newGraph); }
public static GWGraph <NodeData, EdgeData, GraphData> Torus <NodeData, EdgeData, GraphData>(int dimX, int dimY) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("Torus (" + dimX + "," + dimY + ")"); var grid = new GWNode <NodeData, EdgeData, GraphData> [dimX, dimY]; for (int x = 0; x < dimX; x++) { for (int y = 0; y < dimY; y++) { //for (int z = 0; z < numberNodes; z++) { var node = graph.CreateNode(); grid[x, y] = node; } } } for (int x = 0; x < dimX; x++) { for (int y = 0; y < dimY; y++) { //for (int z = 0; z < numberNodes; z++) { if (x < dimX) { graph.CreateEdge(grid[x, y], grid[(x + 1) % dimX, y]); } if (y < dimY) { graph.CreateEdge(grid[x, y], grid[x, (y + 1) % dimY]); } } } } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> DoubleStringEight <NodeData, EdgeData, GraphData>(int numberNodes) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("DoubleStringEight (" + numberNodes + ")"); for (int i = 0; i < numberNodes; i++) { graph.CreateNode(); } var nodes = graph.Nodes.ToList(); for (int i = 0; i < numberNodes; i++) { //for (int k = i; k < numberNodes; k++) { //if (rdm.NextDouble() > 0.9) graph.CreateEdge(nodes[i], nodes[(i + 1) % nodes.Count]); graph.CreateEdge(nodes[i], nodes[(i + 2) % nodes.Count]); if (i == 0) { graph.CreateEdge(nodes[i], nodes[numberNodes / 2]); graph.CreateEdge(nodes[i], nodes[numberNodes / 2 + 1]); graph.CreateEdge(nodes[i + 1], nodes[numberNodes / 2]); graph.CreateEdge(nodes[i + 1], nodes[numberNodes / 2 + 1]); } } } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> GWString <NodeData, EdgeData, GraphData>(int numberNodes) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("String (" + numberNodes + ")"); for (int i = 0; i < numberNodes; i++) { graph.CreateNode(); } var nodes = graph.Nodes.ToList(); for (int i = 0; i < numberNodes; i++) { graph.CreateEdge(nodes[i], nodes[(i + 1) % nodes.Count]); } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> Helix <NodeData, EdgeData, GraphData>(int numberNodes, int connectionSkip) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("Helix (" + connectionSkip + "," + numberNodes + ")"); for (int i = 0; i < numberNodes; i++) { graph.CreateNode(); } var nodes = graph.Nodes.ToList(); for (int i = 0; i < numberNodes; i++) { graph.CreateEdge(nodes[i], nodes[(i + 1) % nodes.Count]); graph.CreateEdge(nodes[i], nodes[(i + connectionSkip) % nodes.Count]); } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> Cube <NodeData, EdgeData, GraphData>(int dimX, int dimY, int dimZ) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("Cube (" + dimX + "x" + dimY + "x" + dimZ + ")"); var cube = new GWNode <NodeData, EdgeData, GraphData> [dimZ][, ]; for (int z = 0; z < dimZ; z++) { cube[z] = new GWNode <NodeData, EdgeData, GraphData> [dimX, dimY]; } for (int x = 0; x < dimX; x++) { for (int y = 0; y < dimY; y++) { for (int z = 0; z < dimZ; z++) { var node = graph.CreateNode(); cube[z][x, y] = node; } } } for (int x = 0; x < dimX; x++) { for (int y = 0; y < dimY; y++) { for (int z = 0; z < dimZ; z++) { if (x < dimX - 1) { graph.CreateEdge(cube[z][x, y], cube[z][x + 1, y]); } if (y < dimY - 1) { graph.CreateEdge(cube[z][x, y], cube[z][x, y + 1]); } if (z < dimZ - 1) { graph.CreateEdge(cube[z][x, y], cube[z + 1][x, y]); } } } } return(graph); }
public static GWGraph <SSPND, SSPED, SSPGD> Do(string nodesFile, string edgesFile) { var graph = new GWGraph <SSPND, SSPED, SSPGD>(); var nodeDict = new Dictionary <string, GWNode <SSPND, SSPED, SSPGD> >(); using (var reader = new StreamReader(nodesFile)) { var line = reader.ReadLine(); while ((line = reader.ReadLine()) != null) { var words = line.Split(','); var nodeName = words[0]; var category = words[1]; var value = double.Parse(words[2], CultureInfo.InvariantCulture); var newNode = graph.CreateNode(); nodeDict.Add(nodeName, newNode); newNode.Data = new SSPND(nodeName, category, value); } } using (var reader = new StreamReader(edgesFile)) { var line = reader.ReadLine(); while ((line = reader.ReadLine()) != null) { var words = line.Split(','); var nodename1 = words[0]; var nodename2 = words[1]; var weight = double.Parse(words[2], CultureInfo.InvariantCulture); var type = words[3]; var node1 = nodeDict[nodename1]; var node2 = nodeDict[nodename2]; var edge = graph.CreateEdge(node1, node2); edge.Data = new SSPED(weight, type); } } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> Grid <NodeData, EdgeData, GraphData>(int dimensionX, int dimensionY) { var graph = new GWGraph <NodeData, EdgeData, GraphData>("Grid (" + dimensionX + "," + dimensionY + ")"); var grid = new GWNode <NodeData, EdgeData, GraphData> [dimensionX, dimensionY]; for (int x = 0; x < dimensionX; x++) { for (int y = 0; y < dimensionY; y++) { //for (int z = 0; z < numberNodes; z++) { var node = graph.CreateNode(); grid[x, y] = node; } } } for (int x = 0; x < dimensionX; x++) { for (int y = 0; y < dimensionY; y++) { //for (int z = 0; z < numberNodes; z++) { if (x < dimensionX - 1) { graph.CreateEdge(grid[x, y], grid[x + 1, y]); } if (y < dimensionY - 1) { graph.CreateEdge(grid[x, y], grid[x, y + 1]); } } } } return(graph); }
public GWGraph <SGLNodeData, SGLEdgeData, SGLGraphData> CreateGraph(ErdösGraphCreationParameter parameter) { GWGraph <SGLNodeData, SGLEdgeData, SGLGraphData> graph = new GWGraph <SGLNodeData, SGLEdgeData, SGLGraphData>(); graph.Data = new SGLGraphData(); graph.Data.NumberCategories = parameter.NumberCategories; for (int i = 0; i < parameter.NumberNodes; i++) { graph.CreateNode(); } //put each node in category s.t. no empty categories bool hasEmptyCategory = true; while (hasEmptyCategory) { List <int> categoriesUsed = new List <int>(); for (int i = 0; i < parameter.NumberCategories; i++) { categoriesUsed.Add(i); } foreach (var node in graph.Nodes) { node.Data = new SGLNodeData(); //put node in random category var category = random.Next(parameter.NumberCategories); node.Data.Category = category; categoriesUsed.Remove(category); } if (categoriesUsed.Count == 0) { hasEmptyCategory = false; } } var nodes = graph.Nodes.ToList(); //connect nodes for (int i = 1; i < nodes.Count; i++) { GWNode <SGLNodeData, SGLEdgeData, SGLGraphData> currentNode = nodes[i]; //create edge to previous nodes with defined probabilty for (int j = 0; j < i; j++) { if (currentNode.Data.Category == nodes[j].Data.Category) { double probabilty = random.NextDouble(); if (probabilty <= parameter.IntraConnectivityDegree) { GWEdge <SGLNodeData, SGLEdgeData, SGLGraphData> edge = graph.CreateEdge(currentNode, nodes[j]); edge.Data = new SGLEdgeData(); edge.Data.Type = EdgeType.Intra; } } //nodes are in different category else if (currentNode.Data.Category != nodes[j].Data.Category) { double probabilty = random.NextDouble(); if (probabilty <= parameter.InterConnectivityDegree) { GWEdge <SGLNodeData, SGLEdgeData, SGLGraphData> edge = graph.CreateEdge(currentNode, nodes[j]); edge.Data = new SGLEdgeData(); edge.Data.Type = EdgeType.Inter; } } } } return(graph); }
public static GWGraph <NodeData, EdgeData, GraphData> CategoryGraph <NodeData, EdgeData, GraphData>(int numberNodes, int categorieCount, Func <NodeData> createNodeData = null) where NodeData : ICategoryNodeData, new() where EdgeData : new() where GraphData : ICategoryGraph, new() { var graph = new GWGraph <NodeData, EdgeData, GraphData>("CategoryGraph (" + numberNodes + "," + categorieCount + ")"); graph.Data = new GraphData(); graph.Data.NumberCategories = categorieCount; for (int i = 0; i < numberNodes; i++) { graph.CreateNode(); } var nodes = graph.Nodes.ToList(); if (createNodeData != null) { nodes.ForEach(n => n.Data = createNodeData()); } var categories = new List <GWNode <NodeData, EdgeData, GraphData> > [categorieCount]; var catsTemp = new int[nodes.Count]; for (int c = 0; c < categorieCount; c++) { categories[c] = new List <GWNode <NodeData, EdgeData, GraphData> >(); } for (int i = 0; i < numberNodes; i++) { var category = rdm.Next(categorieCount); catsTemp[i] = category; categories[category].Add(nodes[i]); nodes[i].Data.Category = category; } for (int i = 0; i < numberNodes; i++) { for (int k = 0; k < 3; k++) { var nb = categories[catsTemp[i]][rdm.Next(categories[catsTemp[i]].Count)]; if (nb.GraphId != i) { graph.CreateEdge(nodes[i], nb); } } if (rdm.NextDouble() < 0.15) { var nb = nodes[rdm.Next(nodes.Count)]; if (nb.GraphId != i) { graph.CreateEdge(nodes[i], nb); } } } foreach (var edge in graph.Edges) { edge.Data = new EdgeData(); } return(graph); }
public static GWGraph <CRFNode3DInfo, CRFEdge3DInfo, Graph3DInfo> DoCRF(string file, int timepoint, double minWeight) { var graph = new GWGraph <CRFNode3DInfo, CRFEdge3DInfo, Graph3DInfo>(); var nodeDict = new Dictionary <string, GWNode <CRFNode3DInfo, CRFEdge3DInfo, Graph3DInfo> >(); using (var reader = new StreamReader(file)) { var rdm = new Random(); var line = reader.ReadLine(); while ((line = reader.ReadLine()) != null) { var words = line.Split(';'); var time = double.Parse(words[0], CultureInfo.InvariantCulture); if (time != timepoint) { continue; } var nodeName = words[1].Substring(1, words[1].Length - 2); var label = words[2].Substring(1, words[2].Length - 2); var labelValue = double.Parse(words[3], CultureInfo.InvariantCulture); var node = default(GWNode <CRFNode3DInfo, CRFEdge3DInfo, Graph3DInfo>); if (!nodeDict.ContainsKey(nodeName)) { node = graph.CreateNode(); node.Data = new CRFNode3DInfo(); node.Data.X = rdm.NextDouble(); node.Data.Y = rdm.NextDouble(); node.Data.Z = rdm.NextDouble(); nodeDict.Add(nodeName, node); } else { node = nodeDict[nodeName]; } var othernodesNames = words[4].Substring(1, words[4].Length - 2).Split(','); if (othernodesNames.Length > 1) { for (int k = 0; k < othernodesNames.Length; k += 2) { var otherNodeName = othernodesNames[k]; var weight = double.Parse(othernodesNames[k + 1], CultureInfo.InvariantCulture); var othernode = default(GWNode <CRFNode3DInfo, CRFEdge3DInfo, Graph3DInfo>); if (!nodeDict.ContainsKey(otherNodeName)) { othernode = graph.CreateNode(); othernode.Data = new CRFNode3DInfo(); othernode.Data.X = rdm.NextDouble(); othernode.Data.Y = rdm.NextDouble(); othernode.Data.Z = rdm.NextDouble(); nodeDict.Add(otherNodeName, othernode); } else { othernode = nodeDict[otherNodeName]; } if (!node.Neighbours.Contains(othernode)) { if (weight >= minWeight) { var newEdge = graph.CreateEdge(node, othernode); newEdge.Data = new CRFEdge3DInfo(); newEdge.Data.Weight = weight; } } } } } } return(graph); }
public static GWGraph <Node3DInfo, Edge3DInfo, Graph3DInfo> Do(string file) { var graph = new GWGraph <Node3DInfo, Edge3DInfo, Graph3DInfo>(); var nodeDict = new Dictionary <string, GWNode <Node3DInfo, Edge3DInfo, Graph3DInfo> >(); var maxNodes = 1000; using (var reader = new StreamReader(file)) { var tempLines = new LinkedList <AgO <string, double> >(); var currentNode = ""; var rdm = new Random(); var line = reader.ReadLine(); while ((line = reader.ReadLine()) != null) { var words = line.Split(','); var nodeName = words[0].Substring(1, words[0].Length - 2); if (!nodeName.EndsWith(".cpp")) { continue; } var othernodeName = words[1].Substring(1, words[1].Length - 2); if (!othernodeName.EndsWith(".cpp")) { continue; } if (currentNode.Equals(words[0])) { tempLines.Add(new AgO <string, double>(line, double.Parse(words[2].Substring(1, words[2].Length - 2), CultureInfo.InvariantCulture))); continue; } else { currentNode = words[0]; } foreach (var tempLine in tempLines.OrderByDescending(tl => tl.Data2).Take(CreateEdgesToNbs)) { words = tempLine.Data1.Split(','); var weight = double.Parse(words[2].Substring(1, words[2].Length - 2), CultureInfo.InvariantCulture); var node = default(GWNode <Node3DInfo, Edge3DInfo, Graph3DInfo>); if (!nodeDict.ContainsKey(nodeName)) { if (nodeDict.Count < maxNodes) { node = graph.CreateNode(); node.Data = new Node3DInfo(); node.Data.X = rdm.NextDouble(); node.Data.Y = rdm.NextDouble(); node.Data.Z = rdm.NextDouble(); nodeDict.Add(nodeName, node); } else { continue; } } else { node = nodeDict[nodeName]; } var othernode = default(GWNode <Node3DInfo, Edge3DInfo, Graph3DInfo>); if (!nodeDict.ContainsKey(othernodeName)) { if (nodeDict.Count < maxNodes) { othernode = graph.CreateNode(); othernode.Data = new Node3DInfo(); othernode.Data.X = rdm.NextDouble(); othernode.Data.Y = rdm.NextDouble(); othernode.Data.Z = rdm.NextDouble(); nodeDict.Add(othernodeName, othernode); } else { continue; } } else { othernode = nodeDict[othernodeName]; } if (!node.Neighbours.Contains(othernode)) { if (weight >= 3.0) { var newEdge = graph.CreateEdge(node, othernode); newEdge.Data = new Edge3DInfo(); newEdge.Data.Weight = weight; } } } tempLines.Clear(); tempLines.Add(new AgO <string, double>(line, double.Parse(words[2].Substring(1, words[2].Length - 2), CultureInfo.InvariantCulture))); } } return(graph); }
public void CreateCategoryGraph(GWGraph <SGLNodeData, SGLEdgeData, SGLGraphData> erdösGraph) { GWGraph <CGNodeData, CGEdgeData, CGGraphData> categoryGraph = new GWGraph <CGNodeData, CGEdgeData, CGGraphData>(); categoryGraph.Data = new CGGraphData(); categoryGraph.Data.ErdösGraph = erdösGraph; //create nodes for (int i = 0; i < erdösGraph.Data.NumberCategories; i++) { var node = categoryGraph.CreateNode(); node.Data = new CGNodeData(); node.Data.Category = i; node.Data.ObservationProbabilty = random.NextDouble(); node.Data.Nodes = new List <GWNode <SGLNodeData, SGLEdgeData, SGLGraphData> >(); node.Data.InterEdges = new List <IGWEdge <SGLNodeData, SGLEdgeData, SGLGraphData> >(); } //count nodes in each category var categoryNodes = categoryGraph.Nodes.ToList(); foreach (var node in erdösGraph.Nodes) { int category = node.Data.Category; categoryNodes[category].Data.NumberNodes++; categoryNodes[category].Data.Nodes.Add(node); } //create edges foreach (var edge in erdösGraph.Edges) { int footCategory = edge.Foot.Data.Category; int headCategory = edge.Head.Data.Category; //edge between categories -> find out if an edge already exists between the 2 categories, if not create it. increment weight in both cases if (edge.Data.Type == EdgeType.Inter) { //add edges to incoming edges of nodes var head = categoryNodes.Find(n => (n.Data.Category == headCategory)); head.Data.InterEdges.Add(edge); var foot = categoryNodes.Find(n => (n.Data.Category == footCategory)); foot.Data.InterEdges.Add(edge); var categoryEdges = categoryGraph.Edges.ToList(); var categoryEdge = categoryEdges.Find(e => (e.Foot.Data.Category == footCategory) && (e.Head.Data.Category == headCategory)); if (categoryEdge == null) { categoryEdge = categoryEdges.Find(e => (e.Foot.Data.Category == headCategory) && (e.Head.Data.Category == footCategory)); } //edge is new if (categoryEdge == null) { var newEdge = categoryGraph.CreateEdge( categoryNodes.Find(n => n.Data.Category == footCategory), categoryNodes.Find(n => n.Data.Category == headCategory)); newEdge.Data = new CGEdgeData(); newEdge.Data.Weight++; } //edge already exists else { categoryEdge.Data.Weight++; } } //edge inside a category -> increment NumberEdges else if (edge.Data.Type == EdgeType.Intra) { if (footCategory != headCategory) { throw new ArgumentException("Conflicting Graph Data: Node " + edge.Foot.GraphId + " and Node " + edge.Head.GraphId + " have to be in the same category"); } categoryNodes.Find(n => n.Data.Category == footCategory).Data.NumberEdges++; } } erdösGraph.Data.CategoryGraph = categoryGraph; }
public static GWGraph <SSPND, SSPED, SSPGD> CGR(string graphFile) { var graph = new GWGraph <SSPND, SSPED, SSPGD>(); var nodeDict = new Dictionary <string, GWNode <SSPND, SSPED, SSPGD> >(); using (var reader = new StreamReader(graphFile)) { var line = reader.ReadLine(); if (!line.Equals("#graph#")) { throw new ArgumentException("File has to start with #graph#"); } { var name = reader.ReadLine(); graph.Name = name.Substring(5); //remove "name=" } while ((line = reader.ReadLine()) != "#nodes#") { continue; } line = reader.ReadLine(); //Id, Category, Value while (!((line = reader.ReadLine()).Equals("#edges#"))) { try { if (string.IsNullOrWhiteSpace(line)) { continue; } var words = line.Split(','); var nodeName = words[0]; var category = words[1]; var value = double.Parse(words[2], CultureInfo.InvariantCulture); var newNode = graph.CreateNode(); nodeDict.Add(nodeName, newNode); newNode.Data = new SSPND(nodeName, category, value); } catch (Exception e) { Log.Error(e); } } if (line != null) { line = reader.ReadLine(); //Source,Target,Weight,Type while ((line = reader.ReadLine()) != null && !line.NullOrEmpty() && !line.Equals("#statistics#")) { line = line.Replace("\"", ""); var words = line.Split(','); var nodename1 = words[0]; var nodename2 = words[1]; var weight = double.Parse(words[2], CultureInfo.InvariantCulture); var type = words[3]; var node1 = nodeDict[nodename1]; var node2 = nodeDict[nodename2]; var edge = graph.CreateEdge(node1, node2); edge.Data = new SSPED(weight, type); } } graph.Data = new SSPGD(); graph.Data.Categories = graph.Nodes.Select(n => n.Data.Category).Distinct().ToList(); CreateCategoryGraph.Do(graph); } return(graph); }