Skip to content

[WIP] Added data structure for graphs #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pydatastructs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .linear_data_structures import *
from .trees import *
from .graphs import *
from .miscellaneous_data_structures import *
from .utils import *
7 changes: 7 additions & 0 deletions pydatastructs/graphs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__all__ = []

from . import graphs
from .graphs import (
Graph
)
__all__.extend(graphs.__all__)
255 changes: 255 additions & 0 deletions pydatastructs/graphs/graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
from __future__ import print_function, division
from pydatastructs.utils.misc_util import Node

__all__ = [
'Graph'
]

class Graph(object):
"""
Abstract representation of graphs.
"""
def __new__(cls, data_structure='adjacency_list'):
if data_structure == 'adjacency_list':
return AdjacencyList()
if data_structure == 'adjacency_matrix':
return AdjacencyMatrix()
return IncidenceMatrix()

def adjacent(self, x, y):
"""
Tests whether there is an edge from the vertex x to the vertex y.
"""
raise NotImplementedError()

def neighbors(self, x):
"""
lists all vertices y such that there is an edge from the vertex x to
the vertex y.
"""
raise NotImplementedError()

def add_vertex(self, x):
"""
Adds the vertex with key x.
"""
raise NotImplementedError()

def remove_vertex(self, x):
"""
Removes the vertex with key x, if it is there.
"""
raise NotImplementedError()

def add_edge(self, x, y):
"""
Adds the edge from the vertex with key x to the
vertex with key y, if it is not there.
"""
raise NotImplementedError()

def remove_edge(self, x, y):
"""
Removes the edge from the vertex with key x to
the vertex with key y, if it is there.
"""
raise NotImplementedError()

def get_vertex_value(self, x):
"""
To obtain the value associated with the vertex with key x.
"""
raise NotImplementedError()

def set_vertex_value(self, x, v):
"""
Sets the value associated with the vertex with key x to v.
"""
raise NotImplementedError()

def get_edge_value(self, x, y):
"""
To obtain the value associated with the edge (x<key>, y<key>).
"""
raise NotImplementedError()

def set_edge_value(self, x, y, v):
"""
Sets the value associated with the edge (x<key>, y<key>) to v.
"""
raise NotImplementedError()

class AdjacencyList(Graph):

def __new__(cls):
obj = object.__new__(cls)
obj._map = dict()
obj._edges = dict()
obj._vertices = []
return obj

def adjacent(self, x, y):
"""
Tests whether there is an edge from the vertex with key
x to the vertex with key y.

Parameters
==========

x: A valid python type
key of the starting node.
y: A valid python type
key of the destination node.

Returns
=======

bool
True if there is an edge, False if there is not an
edge and None if vertex with key x is not in the graph.
"""
nx, ny = x, Node(y, None)
if self._map.get(nx, None) is not None:
return ny in self._map[nx]

def neighbors(self, x):
"""
Lists all vertices y such that there is an edge
from the vertex with key x to the vertex with key y.

Parameters
==========

x: A valid python type
key of the starting node.

Returns
=======

list
If vertex with key x is in the graph.
None
In all other cases.
"""
nx = x
if self._map.get(nx, None) is not None:
return self._map[nx]

def add_vertex(self, x):
"""
Adds the vertex with key x, if it is not there.

Parameters
==========

x: A valid python
The key of the new vertex.
"""
nx, node = x, Node(x, None)
if self._map.get(nx, None) is None:
self._map[nx] = []
self._vertices.append(node)

def remove_vertex(self, x):
"""
Removes the vertex x, if it is there.

Parameters
==========

x: A valid python data type
key of the vertex which is to be removed.

"""
nx = x
if self._map.get(nx, None) is not None:
del self._map[nx]
for key in self._map:
for node in self._map[key]:
if node.key == nx:
self._map[key].remove(node)
break

def add_edge(self, x, y):
"""
Adds the edge from the vertex key x to the vertex key y,
if it is not there.

Parameters
==========

x: A valid python type
key of the starting node.
y: A valid python type
key of the destination node.

"""
nx, ny = x, Node(y, None)
self.add_vertex(nx)
_is_node_present = False
for node in self._map[nx]:
if node.key == y:
_is_node_present = True
break
if not _is_node_present:
self._map[nx].append(ny)

def remove_edge(self, x, y):
"""
Removes the edge from the vertex key x to the vertex key y,
if it is there.

Parameters
==========

x: A valid python type
key of the starting node.
y: A valid python type
key of the destination node.
"""
nx, ny = x, y
for node in self._map[nx]:
if node.key == ny:
self._map[nx].remove(node)
break

def get_vertex_value(self, x):
"""
To obtain the value associated with the vertex with key x.

Parameters
==========

x: A valid python data type
key of the vertex which is to be removed.
"""
nx = x
if self._map.get(nx, None) is not None:
for node in self._vertices:
if node.key == nx:
return node.data

def set_vertex_value(self, x, v):
"""
Sets the value associated with the vertex x to v.

Parameters
==========

x: A valid python data type
key of the vertex which is to be removed.
v: A valid python type
value to be associated with the node.
"""
nx = x
if self._map.get(nx, None) is not None:
for i, node in enumerate(self._vertices):
if node.key == nx:
self._vertices[i].data = v


class AdjacencyMatrix(Graph):
pass

class IncidenceMatrix(Graph):
pass
Empty file.
2 changes: 1 addition & 1 deletion pydatastructs/trees/space_partitioning_trees.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydatastructs.utils import Node
from collections import deque as Queue
from pydatastructs.linear_data_structures.arrays import _check_type
from pydatastructs.utils.misc_util import _check_type

__all__ = [
'OneDimensionalSegmentTree'
Expand Down