Reputation: 77
Algorithm: Bridge Detection Algorithm
Require: connected graph G(V,E)
return Bridge Edges
bridgeSet={}
3.for e(u,v) ε E do
G'= Remove e from G
Disconnected = False;
if BFS in G' starting at u does not visit v then
Disconnected = True;
end if
if Disconnected then
bridgeSet= bridgeSet U {e}
end if
end for
Return bridgeSet
Imports all relevant modules / packages (like NetworkX, etc.);
Uses the functionnetworkx.read_gmlto load the co-appearance network of characters of the novel“Les Misérable”. The data are available from the homepage of Prof. Mark Newman, which is onlineat www-personal.umich.edu/ mejn/netdata/;
Calls the implemented bridge detection function on theLes Misérablenetwork, and prints all de-tected bridge edges, one edge per line.
I used networkx. Then read:
G=nx.read_gml('lesmis.gml') print (nx.info(G))
I have the idea but don't know how to implement it on jupyter: copy then remove from the copy so we don't have to do any tricks in implementation. I truly need help and I am stuck...
so far this is my output: Name: Type: Graph Number of nodes: 77 Number of edges: 254 Average degree: 6.5974 False
Upvotes: 0
Views: 333
Reputation: 10020
NetworkX already has an algorithm for bridges searching: networkx.algorithms.bridges.bridges
:
import networkx as nx
G = nx.fast_gnp_random_graph(30, 0.08, directed=False, seed=1)
list(nx.bridges(G))
[(0, 2), (10, 12), (12, 28), (13, 24), (15, 28), (23, 24)]
If you need the source code, you can find it here and here:
@not_implemented_for('multigraph')
@not_implemented_for('directed')
def bridges(G, root=None):
chains = nx.chain_decomposition(G, root=root)
chain_edges = set(chain.from_iterable(chains))
for u, v in G.edges():
if (u, v) not in chain_edges and (v, u) not in chain_edges:
yield u, v
Upvotes: 0