In Java Language
Randomly Generate a Graph
Accept a positive integer n from the keyboard and then create a random undirected graph with n nodes. Suppose the probability that node i and node j are connected by an edge is 0.25 (you can change this probability yourself). If node i and node j are connected by an edge, we suppose the weight of the edge is a random integer within the range of [1, 5n]. We further suppose there is no self-looping edge for each node, and there is one and only one edge between two nodes if these two nodes are indeed connected.
For example, suppose we accepted the integer 5 from the keyboard, we generate the following random graph:
Node list: {1, 2, 3, 4, 5} Edge list: {(1, 2, 3), (1, 3, 6), (1, 4, 5), (2, 3, 7), (3, 4, 6), (3, 5, 5), (4, 5, 2)}
Note that each edge in the edge list has the form (vi, i, w) with the meaning that node vi and vi are connected by an edge with the weight of w.