Given a method to uniformly at random choose real numbers between 0 and 1, how would you use this random number generator to uniformly at random pick an edge in a graph G with n edges.
I know you can create a random graph G with the random number generator but I'm confused as how that can be modified to pick an random edge in a specific graph G.
Another question comes to mind. Given the graph G was now weighted, how would that algorithm change. I suppose now the weights would have more of a bearing on the edge chosen but how much would it change the algorithm
Any insights?
For the un-weighted graph, assuming that edges are stored in a list, you could simply generate a uniform random integer between 1 and n. Most libraries (in any programming language) will have a uniform random number generator between 0 & 1 u = U(0,1).
Now divide the (0,1) interval uniformly into n sub-intervals. for 'k' ranging from 1 to n-1 check in which interval (k/n, (k+1)/n) does u fall. k is your random edge.
For weighted graph, if the goal is to select an edge randomly based on its weight (higher weight implies more probable for selection), then use the same method as above. But divide the (0,1) interval into sub-interval based on their weights. Finally generate u and select k.
For ex: if there are 3 edges with weights {1,2,3}. Then divide (0,1) interval into 3 sub-interval in that proportion. ie: (0,1/6), (1/6,1/2) and (1/2,1). Generate u and find the interval in which it lies. Say u=0.45, then it lies in 2nd interval, so choose 2nd edge.
Related
A directed graph G is given with Vertices V and Edges E, representing train stations and unidirectional train routes respectively.
Trains of different train numbers move in between pairs of Vertices in a single direction.
Vertices of G are connected with one another through trains with allotted train numbers.
A hop is defined when a passenger needs to shift trains while moving through the graph. The passenger needs to shift trains only if the train-number changes.
Given two Vertices V1 and V2, how would one go about calculating the minimum number of hops needed to reach V2 starting from V1?
In the above example, the minimum number of hops between Vertices 0 and 3 is 1.
There are two paths from 0 to 3, these are
0 -> 1 -> 2 -> 7-> 3
Hop Count 4
Hop Count is 4 as the passenger has to shift from Train A to B then C and B again.
and
0 -> 5 -> 6 -> 8 -> 7 -> 3
Hop Count 1
Hop Count is 1 as the passenger needs only one train route, B to get from Vertices 0 to 3
Thus the minimum hop count is 1.
Input Examples
Input Graph Creation
Input To be solved
Output Example
Output - Solved with Hop Counts
0 in the Hop Count column implies that the destination can't be reached
Assuming number of different trainIDs is relatively small (like 4 in your example), then I suggest using layered graph approach.
Let number of vertices be N, number of edges M, and number of different trainIDs K.
Let's divide our graph to K graphs. (graphA, graphB, ...)
graphA contains only edges labeled with A, and so on.
Weight of each edge in each of the graphs is 0.
Now create edges between these graphs.
Edge between graphs is representing a 'hop'
grapha[i] connects to graphB[i], graphC[i], ...
Each of these edges has weight 1.
Now for every graph run Dijkstra's shortest path algorithm from V1 in that graph, and read results from V2 in all graphs, take minimal value.
This way minimum of results for running dijkstra's for every graph will be your minimum number of hops.
Memory complexity is O(K*(N+M))
And time complexity is O(K*(((2^K)*N+M)*log(KV)))
(2^K)*N comes from fact that for every 1<=i<=N, vertices graphA[i],graphB[i],... must be connected to each other, and this gives 2^K connections for every i, and (2^K)*N connections in total.
For cases where K is relatively small, like 4 in your example, but N and M are quite big, this algorithm works like a charm. It isn't suitable for situation where K is big though.
I'm not sure if that's clear. Tell me if you need more detailed explanation.
EDIT:
Hope this makes my algorithm more clear.
Black edges have weight 0, and red edges have weight 1.
By using layered graph approach, we translated our special graph into plain weighted graph, so we can just run Dijkstra's algorithm on it.
Sorry for ugly image.
EDIT:
Since max K = 10, we would like to remove 2^K from our time complexity. I believe this can be done by making edges that represent possible hops virtual, instead of physically storing them on adjacency list.
If the graph was unweighted you would just go to the row/column (in degree/out degree) of that vertex and count the 1s. But what about a weighted graph? My professor said just count all the non-zero edges but to my understanding it is possible for an edge to have a weight of zero, correct?
So in short: Given an adjacency matrix of a weighted graph with weights of zero allowed, how can you count the degree of a certain vertex?
It doesn't really make sense to have a zero-weight edge in a weighted graph. That depends on what your weights mean for the system you are modelling, of course.
You could have a graph where some have weights and some do not, potentially. In which case, you can't record this as an adjacency matrix since you can't distinguish between 'no edge' and 'unweighted edge'.
If you really needed to have a graph where some edges have no weight encoded in a matrix, then I guess you could do some kind of simple trick like add 1 to all weights when storing in the matrix, then subtract one when you want to calculate properties over those weights.
Problem :
Form a network, that is, all the bases should be reachable from every base.
One base is reachable from other base if there is a path of tunnels connecting bases.
Bases are suppose based on a 2-D plane having integer coordinates.
Cost of building tunnels between two bases are coordinates (x1,y1) and (x2,y2) is min{ |x1-x2|, |y1-y2| }.
What is the minimum cost such that a network is formed.
1 ≤ N ≤ 100000 // Number of bases
-10^9 ≤ xi,yi ≤ 10^9
Typical Kruskal's minimum spanning tree implementation.But u cannot store (10^5)^2 edges.
So how i should make my cost matrix , how to make a graph so i can apply Kruskal algorithm?
You should not store the whole graph as you don't actually need it. In fact in this case I think Prim's algorithm is more suitable in this case. You will not need all the edges at any single time, instead on each iteration you will update a min dist array of size N. Of course complexity will still be in the order of N**2 but at least memory will not be an issue. Also you can further use the specific way distance is computed to improve the complexity(using some ordered structure to store the points).
I believe the only edges that will ever be used (due to your cost function) will be from each base to at most 4 neighbours. The neighbours to use are the closest point with greater (or equal) x value, the closest point with smaller (or equal) x value, the closest point with greater (or equal) y value, the closest point with smaller (or equal) y value.
You can compute these neighbours efficiently by sorting the points according to each axis and then linking each point with the point ahead and behind it in sorted order.
It does not matter if there is more than one point at a particular value of coordinate.
There will therefore be only O(4n) edges for you to consider with Kruskal's algorithm.
We select two vertices randomly and connect them.
So what is the expected number of edges in the graph when it becomes connected ?
I tried solving it using induction but couldn't reach to an answer.
What will be the right approach to this problem ?
For given number of vertices n and chosen count of edges, you get the probability of the graphs connectedness as the proportion of connected graphs to all graphs.
Number of all graphs is the combination number of m over n * (n - 1).
Asymptotic formula for number of connected graphs is given in The asymptotic number of labeled connected graphs with a given number of vertices and edges by Edward A. Bender,
E. Rodney Canfield, Brendan D. McKay (don't ask me to explain it :-) )
Last, you have to specify what you mean by "expected number" - you have to choose a probability threshold (like 95%) and search for an m where this formula gives a probability higher than this threshold.
I need to generate a Deterministic Finite Automata (DFA), selected from all possible DFAs that satisfy the properties below. The DFA must be selected with uniform distribution.
The DFA must have the following four properties:
The DFA has N nodes.
Each node has 2 outgoing transitions.
Each node is reachable from every other node.
The DFA is chosen with perfectly uniform randomness from all possibilities.
I am not considering labeling of nodes or transitions. If two DFAs have the same unlabeled directed graph they are considered the same.
Here are three algorithms that don't work:
Algorithm #1
Start with a set of N nodes called A.
Choose a node from A and put it in set B.
While there are nodes left in set A
- 3.1 Choose a node x from set A
- 3.2 Choose a node y from set B with less than two outgoing transitions.
- 3.3 Choose a node z from set B
- 3.4 Add a transition from y to x.
- 3.5 Add a transition from x to z
- 3.6 Move x to set B
For each node n in B
- 4.1 While n has less than two outgoing transitions
- 4.2 Choose a node m in B
- 4.3 Add a transition from n to m
Algorithm #2
Start with a directed graph with N vertices and no arcs.
Generate a random permutation of the N vertices to produce a random Hamiltonian cycle, and add it to the graph.
For each vertex add one outgoing arc to a randomly chosen vertex.
Algorithm #3
Start with a directed graph with N vertices and no arcs.
Generate a random directed cycle of some length between N and 2N and add it to the graph.
For each vertex add one outgoing arc to a randomly chosen vertex.
I created algorithm #3 based off of algorithm #2, however, I don't know how to select the random directed cycle to create a uniform distribution. I don't even know if it's possible.
Any help would be greatly appreciated.
If N is small (there are N^(2N) possible sets of arcs that meet the first two conditions, so you would want this to be less than the range of your random number generator) you can generate random DFAs and discard the ones that don't satisfy the reachability condition.