Generating a Binary Matrix in C

  • Thread starter Thread starter gradnu
  • Start date Start date
  • Tags Tags
    Binary Matrix
Click For Summary

Discussion Overview

The discussion revolves around generating a binary matrix in C, specifically focusing on creating random adjacency matrices with a fixed number of entries of 0 and 1. The context includes algorithmic approaches, graph theory implications, and the constraints of ensuring network connectivity.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about generating a random binary matrix with fixed counts of 0s and 1s.
  • Another suggests exploring random number generators and discusses the importance of the seed in generating consistent random sequences.
  • A question is raised regarding whether the size of the matrix is also random.
  • It is clarified that the matrix size is finite and that the goal is to represent a network with a fixed number of nodes and links, ensuring connectivity.
  • One participant asks whether the matrix being generated is an incidence matrix or an adjacency matrix, noting the implications for directed graphs.
  • Another confirms the intention to generate random adjacency matrices and emphasizes their asymmetric nature.
  • A proposed algorithm is shared for generating the adjacency matrix, which involves randomly selecting pairs of indices to set entries to 1, with conditions to avoid duplicates.
  • Another participant points out that nodes can link to themselves and suggests that the proposed method may not guarantee a connected network, recommending an outer loop to verify connectivity.

Areas of Agreement / Disagreement

Participants express varying views on the approach to generating the binary matrix, with some proposing specific algorithms while others raise concerns about connectivity and efficiency. The discussion remains unresolved regarding the best method to ensure a connected network.

Contextual Notes

There are limitations regarding the assumptions made about the nature of the network and the computational complexity involved in ensuring connectivity, which remains unaddressed in the proposed solutions.

gradnu
Messages
21
Reaction score
0
Does anybody know how to generate a random matrix with entries 0 and 1 only(binary matrix). Numbers of 1 and 0 are fixed every time matrix is generated.
Possibly a program in 'C'.

Thanks
 
Technology news on Phys.org
Numbers are not truly random so perhaps you would like to look at random number generators. You could take your seed and square it, then take out the bits. and the square is your new seed. Or think of another mathematical formula for generating random numbers.http://www.cprogramming.com/tutorial/random.html
The number returned by function rand is dependent on the initial value, called a seed that remains the same for each run of a program. This means that the sequence of random numbers that is generated by the program will be exactly the same on each run of the program.

You can change the seed to be the same for the matricies you wish to have the same 1/0.
 
is the size of the matrix random as well?
 
No, size of matrix is finite.
Basically the problem is to generate random networks with fixed number of nodes and fixed number of links. Network is being represented by a binary matrix where the entry '1' means that a link exists between the corresponding row and column and '0' means the link does not exist.
I have to make sure that in generating new network(or matrix) the network should remain connected. It shouldn't break into two or more smaller networks.
I haven't done graph theory so if somebody can help me with some kind of algorithm, it would be great.

Thanks,
 
Are you generating a incidence matrix (matrix is indexed by nodes and vertices; element ij is one if node i is an endpoint of vertex j) or an adjacency matrix (element ij is one if some vertex connects node i to node j)? A network is a directed graph, so the adjacency matrix is asymmetric.

Determining whether a random NxN unitary matrix with M ones represents a digraph is the NP-complete problem called the clique problem.
 
I want to generate random adjacency matrices where element ij is 1 if an edge connects node i to node j. Yes it is asymmetric.
 
gradnu said:
I want to generate random adjacency matrices where element ij is 1 if an edge connects node i to node j. Yes it is asymmetric.

..and you wish to do so with a fixed number of "1"s?

This might not be the most efficient:

initialize A=0

For k ones,
do this k times
{
select randomly i from 1 to n, and j from 1 to n
if A[i,j]=1, go back and get another pair (without changing k)
else set A[i,j]=1
}

If necessary, discard proposed links to oneself (i.e. reject the pair if i=j).
 
Last edited:
Nodes in a network can link to themselves. For example, in finite state machine, it is often best to explicitly represent the time steps during which the machine remains in one state as a transition from that state to that same state.

One problem with this approach is that it most likely will not generate a "network". You could wrap this approach in an outer loop that tests whether the adjacency network represents a single graph. If it doesn't, try again. Determining whether this is the case might take a lot of CPU time (its NP-complete).
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 57 ·
2
Replies
57
Views
6K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K