Skip to content

DiGraph

Kfir Goldfarb edited this page Jan 12, 2021 · 3 revisions

DiGraph class:

this class implement directed weighted graph data structure that contains several nodes (vertex) with edges between them.

In this class we used 3 dictionaries:

  1. nodes- basic dictionary for all the nodes in the graph, the key will be the node_id and the value is NodeData object.
  2. childes- dictionary that will contains in as a key the parent id of node and in the value another dictionary for each key node_id of a child and the value is the weight of the edge between the parent and the child.
  3. parents- dictionary that will contains in as a key the child id of node and in the value another dictionary for each key node_id of a parent and the value is the weight of the edge between the child and the parent.

We can see that the class contains following functions: adding and removing a node, adding and removing an edge between two nodes, returning a dictionary of all the nodes in the graph, return a dictionary of all the nodes connected to (into) node_id, return a dictionary of all the nodes connected from node_id, return the number of changes in the graph (mode count), return the sum of nodes (|V|), return the sun of edges (|E|). This is a DiGraph class use another class for representing nodes in the graph called NodeData, NodeData is a node object class, this class represents a node in a graph and each node has key, position, weight and tag (for use in graph algorithms) and info (use as metadata of the node).

The class has a road-system or communication network in mind - and supports a large number of nodes (over 100,000). The implementation is based on an efficient compact representation.

Class methods:

  1. v_size() - return the number of nodes in the graph (|V|)
  2. e_size() - return the number of edges in the graph (|E|)
  3. get_all_v() - return a dictionary of graph nodes {(node_id : NodeData)}
  4. all_in_edges_of_node(node_id) - return a dictionary of all the nodes connected to node {(parent_id, weight)}
  5. all_in_edges_of_node(node_id) - return a dictionary of all the nodes connected from the node {(child_id, weight)}
  6. get_mc() - return graph mode count
  7. add_edge(node1_id, node2_id, weight) - connect between two nodes with weight
  8. add_node(node_id, position) - adding node to the graph
  9. remove_node(node_id) - remove node from the graph and all the connections with other nodes
  10. remove_edge(node1_id, node2_id) - removing connection between node1_id and node2_id
Clone this wiki locally