本文整理汇总了C#中Graph.AddUndirectedEdge方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.AddUndirectedEdge方法的具体用法?C# Graph.AddUndirectedEdge怎么用?C# Graph.AddUndirectedEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.AddUndirectedEdge方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetUp
public void SetUp()
{
_graph = new Graph<int>();
_graph.AddNode(one);
_graph.AddNode(two);
_graph.AddNode(three);
_graph.AddNode(four);
_graph.AddNode(five);
_graph.AddNode(six);
_graph.AddNode(seven);
_graph.AddUndirectedEdge(one, two, 7);
_graph.AddUndirectedEdge(one, three, 9);
_graph.AddUndirectedEdge(one, six, 14);
_graph.AddUndirectedEdge(two, three, 10);
_graph.AddUndirectedEdge(two, four, 15);
_graph.AddUndirectedEdge(three, four, 11);
_graph.AddUndirectedEdge(three, six, 2);
_graph.AddUndirectedEdge(four, five, 6);
_graph.AddUndirectedEdge(four, seven, 2);
_graph.AddUndirectedEdge(five, six, 9);
}
示例2: should_also_add_the_weight_to_the_edge
public void should_also_add_the_weight_to_the_edge()
{
var graph = new Graph<string>();
graph.AddNode("Berlin");
graph.AddNode("Paris");
graph.AddUndirectedEdge("Berlin", "Paris", 0);
Assert.That(graph.NodeCount, Is.EqualTo(2));
}
示例3: should_add_the_to_from_node_to_each_others_neighborlist
public void should_add_the_to_from_node_to_each_others_neighborlist()
{
var graph = new Graph<string>();
graph.AddNode("Berlin");
graph.AddNode("Paris");
graph.AddUndirectedEdge("Berlin", "Paris", 0);
Assert.That(graph.NodeCount, Is.EqualTo(2));
}
示例4: TestDetectCycle
public void TestDetectCycle()
{
var graph = new Graph<int>();
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
var vertex4 = new Vertex<int>(4);
var vertex5 = new Vertex<int>(5);
var vertex6 = new Vertex<int>(6);
var vertex7 = new Vertex<int>(7);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddVertex(vertex4);
graph.AddVertex(vertex5);
graph.AddVertex(vertex6);
graph.AddVertex(vertex7);
graph.AddUndirectedEdge(vertex1, vertex2);
graph.AddUndirectedEdge(vertex1, vertex3);
graph.AddUndirectedEdge(vertex3, vertex4);
graph.AddUndirectedEdge(vertex3, vertex6);
graph.AddUndirectedEdge(vertex4, vertex5);
graph.AddUndirectedEdge(vertex6, vertex7);
Assert.IsFalse(Graph<int>.DetectCycle(graph));
graph.AddUndirectedEdge(vertex5, vertex6);
Assert.IsTrue(Graph<int>.DetectCycle(graph));
}
示例5: BreadthFirstSearch_FourNodesStartNodeOne_Returns661
public void BreadthFirstSearch_FourNodesStartNodeOne_Returns661()
{
int numberOfTestcases = 1;
int[] nodes_and_edges = new int[2] { 4, 2 };
int[] firstEdge = new int[2] { 1, 2 };
int[] secondEdge = new int[2] { 1, 3 };
int startNode = 1;
Graph<int> graph = new Graph<int>();
for (int i = 0; i < 4; i++)
{
graph.AddNode(i + 1);
}
GraphNodeDictionary<int> nodes = graph.Nodes;
graph.AddUndirectedEdge(nodes[1], nodes[2], 6);
graph.AddUndirectedEdge(nodes[1], nodes[3], 6);
BreadthFirstSearch<int> bfs = new BreadthFirstSearch<int>();
List<int> distances = bfs.GetDistances(graph, graph.Nodes[startNode]);
string result = String.Join(" ", distances);
Assert.AreEqual("6 6 -1", result);
}
示例6: TestDijkstra
public void TestDijkstra()
{
var graph = new Graph<int>();
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
var vertex4 = new Vertex<int>(4);
var vertex5 = new Vertex<int>(5);
var vertex6 = new Vertex<int>(6);
var vertex7 = new Vertex<int>(7);
var vertex8 = new Vertex<int>(8);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddVertex(vertex4);
graph.AddVertex(vertex5);
graph.AddVertex(vertex6);
graph.AddVertex(vertex7);
graph.AddVertex(vertex8);
graph.AddUndirectedEdge(vertex1, vertex2, 45);
graph.AddUndirectedEdge(vertex1, vertex3, 20);
graph.AddUndirectedEdge(vertex2, vertex3, 30);
graph.AddUndirectedEdge(vertex2, vertex4, 48);
graph.AddUndirectedEdge(vertex3, vertex5, 25);
graph.AddUndirectedEdge(vertex4, vertex5, 75);
graph.AddUndirectedEdge(vertex3, vertex6, 100);
graph.AddUndirectedEdge(vertex5, vertex6, 110);
graph.AddUndirectedEdge(vertex5, vertex7, 70);
graph.AddUndirectedEdge(vertex6, vertex8, 15);
Dictionary<int, int> costs = Dijkstra.Run(graph, graph.Vertexes[0]);
foreach (var cost in costs)
{
Console.WriteLine("{0} : {1}", cost.Key, cost.Value);
}
}
示例7: Solve
static void Solve(string[] args)
{
int numberOfTestcases = Convert.ToInt32(Console.ReadLine());
BreadthFirstSearch<int> bfs = new BreadthFirstSearch<int>();
for (int i = 0; i < numberOfTestcases; i++)
{
int[] nodeEdges = ReadStringIntoNumArray(Console.ReadLine());
int numOfNodes = nodeEdges[0];
int numOfEdges = nodeEdges[1];
Graph<int> myGraph = new Graph<int>();
for (int j = 0; j < numOfNodes; j++)
{
myGraph.AddNode(j + 1);
}
GraphNodeDictionary<int> nodes = myGraph.Nodes;
for (int k = 0; k < numOfEdges; k++)
{
int[] edgeVertices = ReadStringIntoNumArray(Console.ReadLine());
int from = edgeVertices[0];
int to = edgeVertices[1];
myGraph.AddUndirectedEdge(nodes[from], nodes[to], 6);
}
int startIndex = Convert.ToInt32(Console.ReadLine());
GraphNode<int> startNode = nodes[startIndex];
bfs.GetDistances(myGraph, startNode);
List<int> distances = new List<int>();
for (int n = 0; n < nodes.Count; n++)
{
distances.Add(nodes[n].Distance);
}
string result = String.Join(" ", distances);
Console.WriteLine(result);
}
}
示例8: Main
static void Main(string[] args)
{
//Initialization stage
Graph graph = new Graph();
Dictionary<int, Node> nodeDictionary = new Dictionary<int, Node>();
bool[] primeNumArray = PrimeNumberOperations.GetAllPrimes(10000);
//First iteration. Initialize graph nodes
for (int i = 1000; i <= 9999; i++)
{
if (primeNumArray[i])
{
//Create node
Node node = new Node(i);
//Add Node to graph
graph.AddNode(node);
//Add node to dictionary
nodeDictionary.Add(i, node);
}
}
//Second iteration. Check potential links between nodes
for (int i = 1000; i <= 9999; i++)
{
if (primeNumArray[i])
{
//foreach potential link - 4321
int digit1, digit2, digit3, digit4;
digit1 = i % 10;
digit2 = (i % 100) / 10;
digit3 = (i % 1000) / 100;
digit4 = i / 1000;
//if link - is prime
int[] potentialLinks = new int[36];
int counter = 0;
for (int j = 1; j <= 9; j++)
{
if (j != digit4)
{
potentialLinks[counter] = 1000 * j + 100 * digit3 + 10 * digit2 + digit1;
counter++;
}
}
for (int j = 0; j <= 9; j++)
{
if (j != digit3)
{
potentialLinks[counter] = 1000 * digit4 + 100 * j + 10 * digit2 + digit1;
counter++;
}
}
for (int j = 0; j <= 9; j++)
{
if (j != digit2)
{
potentialLinks[counter] = 1000 * digit4 + 100 * digit3 + 10 * j + digit1;
counter++;
}
}
for (int j = 0; j <= 9; j++)
{
if (j != digit1)
{
potentialLinks[counter] = 1000 * digit4 + 100 * digit3 + 10 * digit2 + j;
counter++;
}
}
Node nodeFrom = nodeDictionary[i];
//Create edge
for (int k = 0; k < counter; k++)
{
Node nodeTo = null;
if (primeNumArray[potentialLinks[k]])
{
nodeTo = nodeDictionary[potentialLinks[k]];
graph.AddUndirectedEdge(nodeFrom, nodeTo);
}
}
}
}
//Imput and Result output
Console.WriteLine("Please enter a pair of four digit prime numbers or press 'Ctrl+C' to exit");
string input;
//.........这里部分代码省略.........
示例9: BreadthFirstSearch_TextReturnsOutput
public void BreadthFirstSearch_TextReturnsOutput()
{
BreadthFirstSearch<int> bfs = new BreadthFirstSearch<int>();
using (StringReader sr = new StringReader(myText.bfs_input))
{
int numberOfTestcases = Convert.ToInt32(sr.ReadLine());
int[] nodeEdges = ReadStringIntoNumArray(sr.ReadLine());
int numOfNodes = nodeEdges[0];
int numOfEdges = nodeEdges[1];
Graph<int> myGraph = new Graph<int>();
for (int j = 0; j < numOfNodes; j++)
{
myGraph.AddNode(j + 1);
Console.WriteLine(j+1);
}
GraphNodeDictionary<int> nodes = myGraph.Nodes;
for (int k = 0; k < numOfEdges; k++)
{
Console.WriteLine("at edge " + k);
int[] edgeVertices = ReadStringIntoNumArray(sr.ReadLine());
int from = edgeVertices[0];
int to = edgeVertices[1];
Console.WriteLine(from + " " + to);
myGraph.AddUndirectedEdge(nodes[from], nodes[to], 6);
}
int startIndex = Convert.ToInt32(sr.ReadLine());
GraphNode<int> startNode = nodes[startIndex];
List<int> bfs_distance = bfs.GetDistances(myGraph, startNode);
List<int> distances = new List<int>();
for (int n = 0; n < nodes.Count; n++)
{
Console.WriteLine(n);
distances.Add(nodes[n+1].Distance);
}
string result = String.Join(" ", bfs_distance);
Assert.AreEqual(result, "6 6 6 6 12 6 12 6 12 12 6 6 6 6 6 12 12 6 6 6 6 12 6 12 6 12 6 12 12 12 12 6 12 12 6 12 12 6 12 6 12 6 12 12 6 6 12 6 6 6 6 12 12 12 12 6 6 6 12 6 6 12 12 12 12 12 12 6 6");
}
}
示例10: Main
static void Main(string[] args)
{
// Ready the node manager!
var nodeManager = new NodeManager();
// Neo4J client, connect to localhost
GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
// Create a new SQL Connection
SqlConnection sqlConnection1 = new SqlConnection("data source=streamweaver.cd08blmpg4sw.us-east-1.rds.amazonaws.com;initial catalog=EarlyAccess;user id=reporting-user;Password=ScottTheIntern!");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = sqlConnection1;
// Get friend pairs
cmd.CommandText = @"
SELECT DISTINCT Person.FirstName as lhFName, Person.LastName as lhLName, Person.Id as lhId, Person.FacebookId as lhFB, Friend.PersonId rhId, Friend.FacebookId as rhFB, pFriend.FirstName as rhFName,pFriend.LastName as rhLName
FROM Person
JOIN Friend ON (Friend.PersonId = Person.Id)
JOIN Person pFriend ON (pFriend.FacebookId=Friend.FacebookId)
";
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Create graph structure
Graph<Person> peopleGraph = new Graph<Person>(new NodeList<Person>());
while (reader.Read())
{
// Get left and right nodes in friendships, they certainly exist
var left = nodeManager.CreateIfExists((string)reader["lhFB"], client, (string)reader["lhFName"], (string)reader["lhLName"], peopleGraph);
var right = nodeManager.CreateIfExists((string)reader["rhFB"], client, (string)reader["rhFName"], (string)reader["rhLName"], peopleGraph);
//// Parse the user's first name and last name from Facebook display name.
//string parsedFirstName = (string)reader["Display"];
//int index = 0;
//foreach (char character in parsedFirstName) {
// index++;
// if (character == ' ') {
// break;
// }
//}
//string parsedLastName = parsedFirstName.Substring(index, parsedFirstName.Length - index);
//parsedFirstName = parsedFirstName.Substring(0, index-1);
//Dictionary<string, Object> dictRight = nodeManager.CreateIfExists((string)reader["rhFB"], client, parsedFirstName, parsedLastName, peopleGraph);
//Object tempRight;
//dictRight.TryGetValue("NodeReference", out tempRight);
//NodeReference<Person> right = (NodeReference<Person>)tempRight;
// Create relationship between left and right
client.CreateRelationship(left.NodeReference, new FriendsWith(right.NodeReference));
Person leftPerson = client.Get<Person>(left.NodeReference).Data;
Person rightPerson = client.Get<Person>(right.NodeReference).Data;
//Object tempLeftNode;
//dictLeft.TryGetValue("GraphNode", out tempLeftNode);
//GraphNode<Person> leftNode = (GraphNode<Person>)tempLeftNode;
//Object tempRightNode;
//dictRight.TryGetValue("GraphNode", out tempRightNode);
//GraphNode<Person> rightNode = (GraphNode<Person>)tempRightNode;
peopleGraph.AddUndirectedEdge(left.GraphNode, right.GraphNode);
// Log relationship creation
string leftName = leftPerson.FirstName;
string rightName = rightPerson.FirstName;
Console.WriteLine("Relationship between " + leftName + " and " + rightName + " created.");
}
sqlConnection1.Close();
// New sql command
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandType = System.Data.CommandType.Text;
cmd2.Connection = sqlConnection1;
// Query for all people with no friends :(
cmd2.CommandText = @"SELECT DISTINCT Person.FacebookId, Person.FirstName, Person.LastName FROM Person";
sqlConnection1.Open();
reader = cmd2.ExecuteReader();
// Create nodes without relationships for all people to fill holes left by original query
while (reader.Read())
{
nodeManager.CreateIfExists((string)reader["FacebookId"], client, (string)reader["FirstName"], (string)reader["LastName"], peopleGraph);
}
sqlConnection1.Close();
Console.WriteLine("\n");
Console.WriteLine(BronKerbosch1(new Graph<Person>(new NodeList<Person>()), peopleGraph, new Graph<Person>(new NodeList<Person>())));
}
示例11: TestTraversals
public void TestTraversals()
{
var graph = new Graph<int>();
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
var vertex4 = new Vertex<int>(4);
var vertex5 = new Vertex<int>(5);
var vertex6 = new Vertex<int>(6);
var vertex7 = new Vertex<int>(7);
var vertex8 = new Vertex<int>(8);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddVertex(vertex4);
graph.AddVertex(vertex5);
graph.AddVertex(vertex6);
graph.AddVertex(vertex7);
graph.AddVertex(vertex8);
graph.AddUndirectedEdge(vertex1, vertex2, 45);
graph.AddUndirectedEdge(vertex1, vertex3, 20);
graph.AddUndirectedEdge(vertex2, vertex3, 30);
graph.AddUndirectedEdge(vertex2, vertex4, 48);
graph.AddUndirectedEdge(vertex3, vertex5, 25);
graph.AddUndirectedEdge(vertex4, vertex5, 75);
graph.AddUndirectedEdge(vertex3, vertex6, 100);
graph.AddUndirectedEdge(vertex5, vertex6, 90);
graph.AddUndirectedEdge(vertex5, vertex7, 70);
graph.AddUndirectedEdge(vertex6, vertex8, 15);
List<int> nodes = graph.Bfs();
Assert.That(nodes, Is.EquivalentTo(new []{1, 2, 3, 4, 5, 6, 7, 8}));
nodes = graph.Dfs();
Assert.That(nodes, Is.EquivalentTo(new[] { 1, 2, 4, 5, 3, 6, 8, 7 }));
}
示例12: TestTraversalsWithUnconnectedComponents
public void TestTraversalsWithUnconnectedComponents()
{
var graph = new Graph<int>();
graph.AddVertex(1);
graph.AddVertex(2);
graph.AddVertex(3);
graph.AddVertex(4);
graph.AddVertex(5);
graph.AddVertex(6);
graph.AddVertex(7);
graph.AddUndirectedEdge(1, 2);
graph.AddUndirectedEdge(2, 3);
graph.AddUndirectedEdge(3, 4);
graph.AddUndirectedEdge(2, 4);
graph.AddUndirectedEdge(5, 6);
graph.AddUndirectedEdge(6, 7);
List<int> nodes = graph.Bfs();
Assert.That(nodes, Is.EquivalentTo(new[] { 1, 2, 3, 4, 5, 6, 7 }));
nodes = graph.Dfs();
Assert.That(nodes, Is.EquivalentTo(new[] { 1, 2, 3, 4, 5, 6, 7 }));
}
示例13: TestTraversalsWithAnotherGraph
public void TestTraversalsWithAnotherGraph()
{
var graph = new Graph<int>();
graph.AddVertex(1);
graph.AddVertex(2);
graph.AddVertex(3);
graph.AddVertex(4);
graph.AddVertex(5);
graph.AddVertex(6);
graph.AddVertex(7);
graph.AddVertex(8);
graph.AddUndirectedEdge(1, 2);
graph.AddUndirectedEdge(1, 3);
graph.AddUndirectedEdge(2, 3);
graph.AddUndirectedEdge(2, 4);
graph.AddUndirectedEdge(3, 5);
graph.AddUndirectedEdge(3, 6);
graph.AddUndirectedEdge(4, 5);
graph.AddUndirectedEdge(5, 6);
graph.AddUndirectedEdge(5, 7);
graph.AddUndirectedEdge(6, 8);
List<int> nodes = graph.Bfs();
Assert.That(nodes, Is.EquivalentTo(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }));
nodes = graph.Dfs();
Assert.That(nodes, Is.EquivalentTo(new[] { 1, 2, 3, 5, 6, 8, 7, 4 }));
}