graph icon indicating copy to clipboard operation
graph copied to clipboard

Some problem with atypical graph

Open Paulo-21 opened this issue 1 year ago • 4 comments

Hello,

I have some trouble with your librairy with graph like this one :

Node(0).
Node(1).
Node(2).
Node(3).
Node(4).
Node(5).
Node(6).
Node(7).
Node(8).
Node(9).
Node(10).
Node(11).
Node(12).
Node(13).
Node(14).
edge(12,9).
edge(11,6).
edge(5,4).
edge(12,7).

As you can see some nodes exist but aren't linked to any nodes. I can't add them to the graph by the edges() function. I'm also try to compute the page rank of this graph. Is there is a trick i can do like when a node is alone it has always the same ranking or something, so that i don't have to had it it the graph ? Or can we find a solution about that ?

Thank you

Paulo-21 avatar Mar 25 '24 22:03 Paulo-21

Hi,

I believe your situation is similar to #123 -- You want to have a certain number of nodes regardless of whether they are connected. You can use the same trick I mentioned in that issue -- adding a Vec<()> of node_count length as node_values (this doesn't have any additional memory overhead because () is a zero-sized type):

let g: DirectedCsrGraph<u32> = GraphBuilder::new()
    .edges(vec![(12, 9), (11, 6), (5, 4), (12, 7)])
    .node_values(vec![(); 15])  // <<== add a Vec<()> of size `node_count`
    .build();

assert_eq!(g.node_count(), 15);

knutwalker avatar Mar 26 '24 14:03 knutwalker

Thanks @Paulo-21 for opening the issue and thanks @knutwalker for sharing the workaround.

Let's add a node_count method on the builder which can be used to override the highest node id that we internally infer from the edges. I assume this is a rather common use case. wdyt?

s1ck avatar Mar 26 '24 15:03 s1ck

Yes, sounds like a great idea

knutwalker avatar Mar 26 '24 16:03 knutwalker

Hi,

I believe your situation is similar to #123 -- You want to have a certain number of nodes regardless of whether they are connected. You can use the same trick I mentioned in that issue -- adding a Vec<()> of node_count length as node_values (this doesn't have any additional memory overhead because () is a zero-sized type):

let g: DirectedCsrGraph<u32> = GraphBuilder::new()
    .edges(vec![(12, 9), (11, 6), (5, 4), (12, 7)])
    .node_values(vec![(); 15])  // <<== add a Vec<()> of size `node_count`
    .build();

assert_eq!(g.node_count(), 15);

Yes ty, i have seen this issue and it work fine for me

Thanks @Paulo-21 for opening the issue and thanks @knutwalker for sharing the workaround.

Let's add a node_count method on the builder which can be used to override the highest node id that we internally infer from the edges. I assume this is a rather common use case. wdyt?

Perhaps knowing the total number of nodes before building the graph could increase the speed of graph creation because we don't have to reallocate memory when we encounter a node with an id higher up in the list of edges ? I don't know how it works under the hood btw.

Paulo-21 avatar Mar 26 '24 16:03 Paulo-21