halp 1.0 documentation

halp package

«  Welcome to halp’s documentation!   ::   Contents   ::   halp.algorithms package  »

halp package

Submodules

halp.directed_hypergraph module

class halp.directed_hypergraph.DirectedHypergraph[source]

Bases: object

The DirectedHypergraph class provides a directed hypergraph object and associated functions for basic properties of directed hypergraphs.

A directed hypergraph contains nodes and hyperedges. Each hyperedge connects a tail set of nodes to a head set of nodes. The tail and head cannot both be empty.

A node is simply any hashable type. See “add_node” or “add_nodes” for more details.

A directed hyperedge is a tuple of the tail nodes and the head nodes. This class assigns (upon adding) and refers to each hyperedge by an internal ID. See “add_hyperedge” or “add_hyperedges” for more details.

Self-loops are allowed, but parallel (multi) hyperedges are not.

Note:This class uses several data structures to store a directed hypergraph. Since these structures must stay in sync (see: __init__), we highly recommend that only the public methods be used for accessing and modifying the hypergraph.

Examples: Create an empty directed hypergraph (no nodes or hyperedges):

>>> H = DirectedHypergraph()

Add nodes (with or without attributes) to the hypergraph one at a time (see: add_node) or several at a time (see: add_nodes):

>>> H.add_nodes(["A", "B", "C", "D"], {color: "black"})

Add hyperedges (with or without attributes) to the hypergraph one at a time (see: add_hyperedge) or several at a time (see: add_hyperedges):

>>> H.add_hyperedges((["A"], ["B"]), (["A", "B"], ["C", "D"]))

Update attributes of existing nodes and hyperedges by simulating adding the node or hyperedge again, with the [potentially new] attribute appended:

>>> H.add_node("A", label="sink")
>>> H.add_hyperedge((["A", "B"], ["C", "D"]), weight=5)
add_hyperedge(tail, head, attr_dict=None, **attr)[source]

Adds a hyperedge to the hypergraph, along with any related attributes of the hyperedge. This method will automatically add any node from the tail and head that was not in the hypergraph. A hyperedge without a “weight” attribute specified will be assigned the default value of 1.

Parameters:
  • tail – iterable container of references to nodes in the tail of the hyperedge to be added.
  • head – iterable container of references to nodes in the head of the hyperedge to be added.
  • attr_dict – dictionary of attributes shared by all the hyperedges.
  • attr – keyword arguments of attributes of the hyperedge; attr’s values will override attr_dict’s values if both are provided.
Returns:

str – the ID of the hyperedge that was added.

Raises:

ValueError – tail and head arguments cannot both be empty.

Examples:

>>> H = DirectedHypergraph()
>>> x = H.add_hyperedge(["A", "B"], ["C", "D"])
>>> y = H.add_hyperedge(("A", "C"), ("B"), 'weight'=2)
>>> z = H.add_hyperedge(set(["D"]),
                        set(["A", "C"]),
                        {color: "red"})
add_hyperedges(hyperedges, attr_dict=None, **attr)[source]
Adds multiple hyperedges to the graph, along with any related
attributes of the hyperedges. If any node in the tail or head of any hyperedge has not previously been added to the hypergraph, it will automatically be added here. Hyperedges without a “weight” attribute specified will be assigned the default value of 1.
Parameters:
  • hyperedges – iterable container to either tuples of (tail reference, head reference) OR tuples of (tail reference, head reference, attribute dictionary); if an attribute dictionary is provided in the tuple, its values will override both attr_dict’s and attr’s values.
  • attr_dict – dictionary of attributes shared by all the hyperedges.
  • attr – keyword arguments of attributes of the hyperedges; attr’s values will override attr_dict’s values if both are provided.
Returns:

list – the IDs of the hyperedges added in the order specified by the hyperedges container’s iterator.

See also: add_hyperedge

Examples:

>>> H = DirectedHypergraph()
>>> xyz = hyperedge_list = ((["A", "B"], ["C", "D"]),
                            (("A", "C"), ("B"), {'weight': 2}),
                            (set(["D"]), set(["A", "C"])))
>>> H.add_hyperedges(hyperedge_list)
add_node(node, attr_dict=None, **attr)[source]
Adds a node to the graph, along with any related attributes
of the node.
Parameters:
  • node – reference to the node being added.
  • attr_dict – dictionary of attributes of the node.
  • attr – keyword arguments of attributes of the node; attr’s values will override attr_dict’s values if both are provided.

Examples:

>>> H = DirectedHypergraph()
>>> attributes = {label: "positive"}
>>> H.add_node("A", attributes)
>>> H.add_node("B", label="negative")
>>> H.add_node("C", attributes, root=True)
add_nodes(nodes, attr_dict=None, **attr)[source]
Adds multiple nodes to the graph, along with any related attributes
of the nodes.
Parameters:
  • nodes – iterable container to either references of the nodes OR tuples of (node reference, attribute dictionary); if an attribute dictionary is provided in the tuple, its values will override both attr_dict’s and attr’s values.
  • attr_dict – dictionary of attributes shared by all the nodes.
  • attr – keyword arguments of attributes of the node; attr’s values will override attr_dict’s values if both are provided.

See also: add_node

Examples:

>>> H = DirectedHypergraph()
>>> attributes = {label: "positive"}
>>> node_list = ["A",
                 ("B", {label="negative"}),
                 ("C", {root=True})]
>>> H.add_nodes(node_list, attributes)
copy()[source]

Creates a new DirectedHypergraph object with the same node and hyperedge structure. Copies of the nodes’ and hyperedges’ attributes are stored and used in the new hypergraph.

Returns:DirectedHypergraph – a new hypergraph that is a copy of the current hypergraph
get_backward_star(node)[source]

Given a node, get a copy of that node’s backward star.

Parameters:node – node to retrieve the backward-star of.
Returns:set – set of hyperedge_ids for the hyperedges in the node’s backward star.
Raises:ValueError – No such node exists.
get_forward_star(node)[source]

Given a node, get a copy of that node’s forward star.

Parameters:node – node to retrieve the forward-star of.
Returns:set – set of hyperedge_ids for the hyperedges in the node’s forward star.
Raises:ValueError – No such node exists.
get_hyperedge_attribute(hyperedge_id, attribute_name)[source]

Given a hyperedge ID and the name of an attribute, get a copy of that hyperedge’s attribute.

Parameters:
  • hyperedge_id – ID of the hyperedge to retrieve the attribute of.
  • attribute_name – name of the attribute to retrieve.
Returns:

attribute value of the attribute_name key for the specified hyperedge.

Raises:

ValueError – No such hyperedge exists.

Raises:

ValueError – No such attribute exists.

Examples:

>>> H = DirectedHypergraph()
>>> hyperedge_list = (["A"], ["B", "C"]),
                      (("A", "B"), ("C"), {weight: 2}),
                      (set(["B"]), set(["A", "C"])))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> attribute = H.get_hyperedge_attribute(hyperedge_ids[0])
get_hyperedge_attributes(hyperedge_id)[source]

Given a hyperedge ID, get a dictionary of copies of that hyperedge’s attributes.

Parameters:hyperedge_id – ID of the hyperedge to retrieve the attributes of.
Returns:dict – copy of each attribute of the specified hyperedge_id (except the private __frozen_tail and __frozen_head entries).
Raises:ValueError – No such hyperedge exists.
get_hyperedge_head(hyperedge_id)[source]

Given a hyperedge ID, get a copy of that hyperedge’s head.

Parameters:hyperedge – ID of the hyperedge to retrieve the head from.
Returns:a copy of the container of nodes that the user provided as the head to the hyperedge referenced as hyperedge_id.
get_hyperedge_id(tail, head)[source]

From a tail and head set of nodes, returns the ID of the hyperedge that these sets comprise.

Parameters:
  • tail – iterable container of references to nodes in the tail of the hyperedge to be added
  • head – iterable container of references to nodes in the head of the hyperedge to be added
Returns:

str – ID of the hyperedge that has that the specified tail and head sets comprise.

Raises:

ValueError – No such hyperedge exists.

Examples:

>>> H = DirectedHypergraph()
>>> hyperedge_list = (["A"], ["B", "C"]),
                      (("A", "B"), ("C"), {weight: 2}),
                      (set(["B"]), set(["A", "C"])))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> x = H.get_hyperedge_id(["A"], ["B", "C"])
get_hyperedge_id_set()[source]

Returns the set of IDs of hyperedges that are currently in the hypergraph.

Returns:set – all IDs of hyperedges currently in the hypergraph
get_hyperedge_tail(hyperedge_id)[source]

Given a hyperedge ID, get a copy of that hyperedge’s tail.

Parameters:hyperedge_id – ID of the hyperedge to retrieve the tail from.
Returns:a copy of the container of nodes that the user provided as the tail to the hyperedge referenced as hyperedge_id.
get_hyperedge_weight(hyperedge_id)[source]

Given a hyperedge ID, get that hyperedge’s weight.

Parameters:hyperedge – ID of the hyperedge to retrieve the weight from.
Returns:a the weight of the hyperedge referenced as hyperedge_id.
get_induced_subhypergraph(nodes)[source]

Gives a new hypergraph that is the subhypergraph of the current hypergraph induced by the provided set of nodes. That is, the induced subhypergraph’s node set corresponds precisely to the nodes provided, and the coressponding hyperedges in the subhypergraph are only those from the original graph consist of tail and head sets that are subsets of the provided nodes.

Parameters:nodes – the set of nodes to find the induced subhypergraph of.
Returns:DirectedHypergraph – the subhypergraph induced on the provided nodes.
get_node_attribute(node, attribute_name)[source]

Given a node and the name of an attribute, get a copy of that node’s attribute.

Parameters:
  • node – reference to the node to retrieve the attribute of.
  • attribute_name – name of the attribute to retrieve.
Returns:

attribute value of the attribute_name key for the specified node.

Raises:

ValueError – No such node exists.

Raises:

ValueError – No such attribute exists.

get_node_attributes(node)[source]

Given a node, get a dictionary with copies of that node’s attributes.

Parameters:node – reference to the node to retrieve the attributes of.
Returns:dict – copy of each attribute of the specified node.
Raises:ValueError – No such node exists.
get_node_set()[source]

Returns the set of nodes that are currently in the hypergraph.

Returns:set – all nodes currently in the hypergraph
get_predecessors(head)[source]

Given a head set of nodes, get a list of edges of which the node set is the head of each edge.

Parameters:head – set of nodes that correspond to the heads of some (possibly empty) set of edges.
Returns:set – hyperedge_ids of the hyperedges that have head in the head.
get_successors(tail)[source]

Given a tail set of nodes, get a list of edges of which the node set is the tail of each edge.

Parameters:tail – set of nodes that correspond to the tails of some (possibly empty) set of edges.
Returns:set – hyperedge_ids of the hyperedges that have tail in the tail.
get_symmetric_image()[source]

Creates a new DirectedHypergraph object that is the symmetric image of this hypergraph (i.e., identical hypergraph with all edge directions reversed). Copies of each of the nodes’ and hyperedges’ attributes are stored and used in the new hypergraph.

Returns:DirectedHypergraph – a new hypergraph that is the symmetric image of the current hypergraph.
has_hyperedge(tail, head)[source]

Given a tail and head set of nodes, returns whether there is a hyperedge in the hypergraph that connects the tail set to the head set.

Parameters:
  • tail – iterable container of references to nodes in the tail of the hyperedge being checked.
  • head – iterable container of references to nodes in the head of the hyperedge being checked.
Returns:

bool – true iff a hyperedge exists connecting the specified tail set to the specified head set.

has_hyperedge_id(hyperedge_id)[source]

Determines if a hyperedge referenced by hyperedge_id exists in the hypergraph.

Parameters:hyperedge_id – ID of the hyperedge whose existence is being checked.
Returns:bool – true iff a hyperedge exists that has id hyperedge_id.
has_node(node)[source]

Determines if a specific node is present in the hypergraph.

Parameters:node – reference to the node whose presence is being checked.
Returns:bool – true iff the node exists in the hypergraph.
hyperedge_id_iterator()[source]

Provides an iterator over the list of hyperedge IDs.

is_BF_hypergraph()[source]

Indicates whether the hypergraph is a BF-hypergraph. A BF-hypergraph consists of only B-hyperedges and F-hyperedges. See “is_B_hypergraph” or “is_F_hypergraph” for more details.

Returns:bool – True iff the hypergraph is an F-hypergraph.
is_B_hypergraph()[source]

Indicates whether the hypergraph is a B-hypergraph. In a B-hypergraph, all hyperedges are B-hyperedges – that is, every hyperedge has exactly one node in the head.

Returns:bool – True iff the hypergraph is a B-hypergraph.
is_F_hypergraph()[source]

Indicates whether the hypergraph is an F-hypergraph. In an F-hypergraph, all hyperedges are F-hyperedges – that is, every hyperedge has exactly one node in the tail.

Returns:bool – True iff the hypergraph is an F-hypergraph.
node_iterator()[source]

Provides an iterator over the nodes.

read(file_name, delim=', ', sep='t')[source]

Read a directed hypergraph from a file, where nodes are represented as strings. Each column is separated by “sep”, and the individual tail nodes and head nodes are delimited by “delim”. The header line is currently ignored, but columns should be of the format: tailnode1[delim]..tailnodeM[sep]headnode1[delim]..headnodeN[sep]weight

As a concrete example, an arbitrary line with delim=’,’ and sep=’ ‘ (4 spaces) may look like:

x1,x2    x3,x4,x5    12

which defines a hyperedge of weight 12 from a tail set containing nodes “x1” and “x2” to a head set containing nodes “x3”, “x4”, and “x5”

remove_hyperedge(hyperedge_id)[source]

Removes a hyperedge and its attributes from the hypergraph.

Parameters:hyperedge_id – ID of the hyperedge to be removed.
Raises:ValueError – No such hyperedge exists.

Examples:

>>> H = DirectedHypergraph()
>>> xyz = hyperedge_list = ((["A"], ["B", "C"]),
                            (("A", "B"), ("C"), {'weight': 2}),
                            (set(["B"]), set(["A", "C"])))
>>> H.add_hyperedges(hyperedge_list)
>>> H.remove_hyperedge(xyz[0])
remove_hyperedges(hyperedge_ids)[source]

Removes a set of hyperedges and their attributes from the hypergraph.

Parameters:hyperedge_ids – iterable container of IDs of the hyperedges to be removed.
Raises:ValueError – No such hyperedge exists.

See also: remove_hyperedge

Examples:

>>> H = DirectedHypergraph()
>>> hyperedge_list = ((["A"], ["B", "C"]),
                      (("A", "B"), ("C"), {'weight': 2}),
                      (set(["B"]), set(["A", "C"])))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> H.remove_hyperedges(hyperedge_ids)
remove_node(node)[source]

Removes a node and its attributes from the hypergraph. Removes every hyperedge that contains this node in either the head or the tail.

Parameters:node – reference to the node being added.
Raises:ValueError – No such node exists.

Examples:

>>> H = DirectedHypergraph()
>>> H.add_node("A", label="positive")
>>> H.remove_node("A")
remove_nodes(nodes)[source]

Removes multiple nodes and their attributes from the graph. If the nodes are part of any hyperedges, those hyperedges are removed as well.

Parameters:nodes – iterable container to either references of the nodes OR tuples of (node reference, attribute dictionary); if an attribute dictionary is provided in the tuple, its values will override both attr_dict’s and attr’s values.

See also: remove_node

Examples:

>>> H = DirectedHypergraph()
>>> attributes = {label: "positive"}
>>> node_list = ["A",
                 ("B", {label="negative"}),
                 ("C", {root=True})]
>>> H.add_nodes(node_list, attributes)
>>> H.remove_nodes(["A", "B", "C"])
write(file_name, delim=', ', sep='t')[source]

Write a directed hypergraph to a file, where nodes are represented as strings. Each column is separated by “sep”, and the individual tail nodes and head nodes are delimited by “delim”. The header line is currently ignored, but columns should be of the format: tailnode1[delim]..tailnodeM[sep]headnode1[delim]..headnodeN[sep]weight

As a concrete example, an arbitrary line with delim=’,’ and sep=’ ‘ (4 spaces) may look like:

x1,x2    x3,x4,x5    12

which defines a hyperedge of weight 12 from a tail set containing nodes “x1” and “x2” to a head set containing nodes “x3”, “x4”, and “x5”

halp.undirected_hypergraph module

class halp.undirected_hypergraph.UndirectedHypergraph[source]

Bases: object

The UndirectedHypergraph class provides an undirected hypergraph object and associated functions for basic properties of undirected hypergraphs.

An undirected hypergraph contains nodes and undirected hyperedges. Each undirected hyperedge simply connects a set of nodes. An undirected hypergraph is a special case of an undirected graph, where each edge connects exactly 2 nodes. The set of nodes cannot be empty.

A node is simply any hashable type. See “add_node” or “add_nodes” for more details.

An undirected hyperedge is any iterable container of nodes. This class assigns (upon adding) and refers to each hyperedge by an internal ID. See “add_hyperedge” or “add_hyperedges” for more details.

Self-loops and parallel (multi-) hyperedges are not allowed.

Note:This class uses several data structures to store a undirected hypergraph. Since these structures must stay in sync (see: __init__), we highly recommend that only the public methods be used for accessing and modifying the hypergraph.

Examples: Create an empty undirected hypergraph (no nodes or hyperedges).

>>> H = UndirectedHypergraph()

Add nodes (with or without attributes) to the hypergraph one at a time (see: add_node) or several at a time (see: add_nodes).

>>> H.add_nodes(["A", "B", "C", "D"], {color: "black"})

Add hyperedges to the hypergraph one at a time (see: add_hyperedge) or several at a time (see: add_hyperedges).

>>> H.add_hyperedges(["A", "B"], ("A", "C", "D"))

Update attributes of existing nodes and hyperedges by simulating adding the node or hyperedge again, with the [potentially new] attribute appended.

>>> H.add_node("A", label="sink")
>>> H.add_hyperedge(["A", "C", "D"], weight=5)
add_hyperedge(nodes, attr_dict=None, **attr)[source]
Adds a hyperedge to the hypergraph, along with any related
attributes of the hyperedge. This method will automatically add any node from the node set that was not in the hypergraph. A hyperedge without a “weight” attribute specified will be assigned the default value of 1.
Parameters:
  • nodes – iterable container of references to nodes in the hyperedge to be added.
  • attr_dict – dictionary of attributes of the hyperedge being added.
  • attr – keyword arguments of attributes of the hyperedge; attr’s values will override attr_dict’s values if both are provided.
Returns:

str – the ID of the hyperedge that was added.

Raises:

ValueError – nodes arguments cannot be empty.

Examples:

>>> H = UndirectedHypergraph()
>>> x = H.add_hyperedge(["A", "B", "C"])
>>> y = H.add_hyperedge(("A", "D"), weight=2)
>>> z = H.add_hyperedge(set(["B", "D"]), {color: "red"})
add_hyperedges(hyperedges, attr_dict=None, **attr)[source]
Adds multiple hyperedges to the graph, along with any related
attributes of the hyperedges. If any node of a hyperedge has not previously been added to the hypergraph, it will automatically be added here. Hyperedges without a “weight” attribute specified will be assigned the default value of 1.
Parameters:
  • hyperedges – iterable container to references of the node sets
  • attr_dict – dictionary of attributes shared by all the hyperedges being added.
  • attr – keyword arguments of attributes of the hyperedges; attr’s values will override attr_dict’s values if both are provided.
Returns:

list – the IDs of the hyperedges added in the order specified by the hyperedges container’s iterator.

See also: add_hyperedge

Examples:

>>> H = UndirectedHypergraph()
>>> hyperedge_list = (["A", "B", "C"],
                      ("A", "D"),
                      set(["B", "D"]))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
add_node(node, attr_dict=None, **attr)[source]
Adds a node to the graph, along with any related attributes
of the node.
Parameters:
  • node – reference to the node being added.
  • attr_dict – dictionary of attributes of the node.
  • attr – keyword arguments of attributes of the node; attr’s values will override attr_dict’s values if both are provided.
Note:

Following the NetworkX model, we allow both a dictionary of attributes to be passed in by the user as well as key-value pairs of attributes to be passed in by the user to provide greater flexibility. This pattern is followed in methods such as add_nodes, add_hyperedge, and add_hyperedges.

Examples:

>>> H = UndirectedHypergraph()
>>> attributes = {label: "positive"}
>>> H.add_node("A", attributes)
>>> H.add_node("B", label="negative")
>>> H.add_node("C", attributes, root=True)
add_nodes(nodes, attr_dict=None, **attr)[source]
Adds multiple nodes to the graph, along with any related attributes
of the nodes.
Parameters:
  • nodes – iterable container to either references of the nodes OR tuples of (node reference, attribute dictionary); if an attribute dictionary is provided in the tuple, its values will override both attr_dict’s and attr’s values.
  • attr_dict – dictionary of attributes shared by all the nodes.
  • attr – keyword arguments of attributes of the node; attr’s values will override attr_dict’s values if both are provided.

See also: add_node

Examples:

>>> H = UndirectedHypergraph()
>>> attributes = {label: "positive"}
>>> node_list = ["A",
                 ("B", {label="negative"}),
                 ("C", {root=True})]
>>> H.add_nodes(node_list, attributes)
copy()[source]

Creates a new UndirectedHypergraph object with the same node and hyperedge structure. Copies of the nodes’ and hyperedges’ attributes are stored and used in the new hypergraph.

Returns:UndirectedHypergraph – a new hypergraph that is a copy of the current hypergraph
get_hyperedge_attribute(hyperedge_id, attribute_name)[source]

Given a hyperedge ID and the name of an attribute, get a copy of that hyperedge’s attribute.

Parameters:
  • hyperedge_id – ID of the hyperedge to retrieve the attribute of.
  • attribute_name – name of the attribute to retrieve.
Returns:

attribute value of the attribute_name key for the specified hyperedge.

Raises:

ValueError – No such hyperedge exists.

Raises:

ValueError – No such attribute exists.

Examples:

>>> H = UndirectedHypergraph()
>>> hyperedge_list = (["A", "B", "C"],
                      ("A", "D"),
                      set(["B", "D"]))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> attribute = H.get_hyperedge_attribute(hyperedge_ids[0])
get_hyperedge_attributes(hyperedge_id)[source]

Given a hyperedge ID, get a dictionary of copies of that hyperedge’s attributes.

Parameters:hyperedge_id – ID of the hyperedge to retrieve the attributes of.
Returns:dict – copy of each attribute of the specified hyperedge_id (except the private __frozen_nodes entry).
Raises:ValueError – No such hyperedge exists.
get_hyperedge_id(nodes)[source]

From a set of nodes, returns the ID of the hyperedge that this set comprises.

Parameters:nodes – iterable container of references to nodes in the the hyperedge to be added
Returns:str – ID of the hyperedge that has that the specified node set comprises.
Raises:ValueError – No such hyperedge exists.

Examples:

>>> H = UndirectedHypergraph()
>>> hyperedge_list = (["A", "B", "C"],
                      ("A", "D"),
                      set(["B", "D"]))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> x = H.get_hyperedge_id(["A", "B", "C"])
get_hyperedge_id_set()[source]

Returns the set of IDs of hyperedges that are currently in the hypergraph.

Returns:set – all IDs of hyperedges currently in the hypergraph
get_hyperedge_nodes(hyperedge_id)[source]

Given a hyperedge ID, get a copy of that hyperedge’s nodes.

Parameters:hyperedge_id – ID of the hyperedge to retrieve the nodes from.
Returns:a copy of the container of nodes that the user provided for the hyperedge referenced as hyperedge_id.
get_hyperedge_weight(hyperedge_id)[source]

Given a hyperedge ID, get that hyperedge’s weight.

Parameters:hyperedge – ID of the hyperedge to retrieve the weight from.
Returns:int – the weight of the hyperedge referenced as hyperedge_id.
get_node_attribute(node, attribute_name)[source]

Given a node and the name of an attribute, get a copy of that node’s attribute.

Parameters:
  • node – reference to the node to retrieve the attribute of.
  • attribute_name – name of the attribute to retrieve.
Returns:

attribute value of the attribute_name key for the specified node.

Raises:

ValueError – No such node exists.

Raises:

ValueError – No such attribute exists.

get_node_attributes(node)[source]

Given a node, get a dictionary with copies of that node’s attributes.

Parameters:node – reference to the node to retrieve the attributes of.
Returns:dict – copy of each attribute of the specified node.
Raises:ValueError – No such node exists.
get_node_set()[source]

Returns the set of nodes that are currently in the hypergraph.

Returns:set – all nodes currently in the hypergraph
get_star(node)[source]

Given a node, get a copy of that node’s star, that is, the set of hyperedges that the node belongs to.

Parameters:node – node to retrieve the star of.
Returns:set – set of hyperedge_ids for the hyperedges in the node’s star.
Raises:ValueError – No such node exists.
has_hyperedge(nodes)[source]

Given a set of nodes, returns whether there is a hyperedge in the hypergraph that is precisely composed of those nodes.

Parameters:nodes – iterable container of references to nodes in the hyperedge being checked.
Returns:bool – true iff a hyperedge exists composed of the specified nodes.
has_hyperedge_id(hyperedge_id)[source]

Determines if a hyperedge referenced by hyperedge_id exists in the hypergraph.

Parameters:hyperedge_id – ID of the hyperedge whose existence is being checked.
Returns:bool – true iff a hyperedge exists that has id hyperedge_id.
has_node(node)[source]

Determines if a specific node is present in the hypergraph.

Parameters:node – reference to the node whose presence is being checked.
Returns:bool – true iff the node exists in the hypergraph.
hyperedge_id_iterator()[source]

Provides an iterator over the list of hyperedge IDs.

node_iterator()[source]

Provides an iterator over the nodes.

read(file_name, delim=', ', sep='t')[source]

Reads an undirected hypergraph from a file, where nodes are represented as strings. Each column is separated by “sep”, and the individual nodes are delimited by “delim”. The header line is currently ignored, but columns should be of the format: node1[delim]..nodeM[sep]weight

As a concrete example, an arbitrary line with delim=’,’ and sep=’ ‘ (4 spaces) may look like:

x1,x2,x3,x5    12

which defines a hyperedge of weight 12 from a node set containing nodes “x1”, “x2”, “x3”, and “x5”.

remove_hyperedge(hyperedge_id)[source]

Removes a hyperedge and its attributes from the hypergraph.

Parameters:hyperedge_id – ID of the hyperedge to be removed.
Raises:ValueError – No such hyperedge exists.

Examples:

>>> H = UndirectedHypergraph()
>>> hyperedge_list = (["A", "B", "C"],
                      ("A", "D"),
                      set(["B", "D"]))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> H.remove_hyperedge(hyperedge_ids[0])
>>> BD_id = H.get_hyperedge_id(set(["B", "D"]))
>>> H.remove_hyperedge(BD_id)
remove_hyperedges(hyperedge_ids)[source]

Removes a set of hyperedges and their attributes from the hypergraph.

Parameters:hyperedge_ids – iterable container of IDs of the hyperedges to be removed.
Raises:ValueError – No such hyperedge exists.

See also: remove_hyperedge

Examples:

>>> H = UndirectedHypergraph()
>>> hyperedge_list = (["A", "B", "C"],
                      ("A", "D"),
                      set(["B", "D"]))
>>> hyperedge_ids = H.add_hyperedges(hyperedge_list)
>>> H.remove_hyperedges(hyperedge_ids)
remove_node(node)[source]

Removes a node and its attributes from the hypergraph. Removes every hyperedge that contains this node.

Parameters:node – reference to the node being added.
Raises:ValueError – No such node exists.

Examples:

>>> H = UndirectedHypergraph()
>>> H.add_node("A", label="positive")
>>> H.remove_node("A")
remove_nodes(nodes)[source]

Removes multiple nodes and their attributes from the graph. If the nodes are part of any hyperedges, those hyperedges are removed as well.

Parameters:nodes – iterable container to either references of the nodes OR tuples of (node reference, attribute dictionary); if an attribute dictionary is provided in the tuple, its values will override both attr_dict’s and attr’s values.

See also: remove_node

Examples:

>>> H = UndirectedHypergraph()
>>> attributes = {label: "positive"}
>>> node_list = ["A",
                 ("B", {label="negative"}),
                 ("C", {root=True})]
>>> H.add_nodes(node_list, attributes)
>>> H.remove_nodes(["A", "B", "C"])
write(file_name, delim=', ', sep='t')[source]

Writes an undirected hypergraph from a file, where nodes are represented as strings. Each column is separated by “sep”, and the individual nodes are delimited by “delim”. The header line is currently ignored, but columns should be of the format: node1[delim]..nodeM[sep]node1[delim]..nodeN[sep]weight

As a concrete example, an arbitrary line with delim=’,’ and sep=’ ‘ (4 spaces) may look like:

x1,x2,x3,x5    12

which defines a hyperedge of weight 12 from a node set containing nodes “x1”, “x2”, “x3”, and “x5”.

Module contents

«  Welcome to halp’s documentation!   ::   Contents   ::   halp.algorithms package  »