0% found this document useful (0 votes)
26 views5 pages

Init - : Directedgraph: (Number - of - Vertices)

This document describes a DirectedGraph class that represents directed graphs. The class initializes graphs with dictionaries to store predecessor, successor, and edge cost information. It provides various methods to access graph properties and manipulate graph elements like vertices, edges, and edge information. These methods allow checking if vertices and edges exist, iterating over graph elements, and modifying the graph by adding, removing, or updating vertices and edges. The class also supports copying graphs and reading/writing them to files.

Uploaded by

Pop Robert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Init - : Directedgraph: (Number - of - Vertices)

This document describes a DirectedGraph class that represents directed graphs. The class initializes graphs with dictionaries to store predecessor, successor, and edge cost information. It provides various methods to access graph properties and manipulate graph elements like vertices, edges, and edge information. These methods allow checking if vertices and edges exist, iterating over graph elements, and modifying the graph by adding, removing, or updating vertices and edges. The class also supports copying graphs and reading/writing them to files.

Uploaded by

Pop Robert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

class DirectedGraph:

def __init__(self, number_of_vertices):


"""
Representation:
- three dictionaries:
- one containing the inbound vertices of every vertex
(self._dict_predecessors)
- one containing the outbound vertices of every vertex
(self._dict_successors)
- one containing the cost of every edge (self._dict_costs)
"""

def is_vertex(self,vertex):
"""
Description:
- checks if "vertex" is a vertex of the graph
Input:
- vertex being checked
Output:
- returns true if "vertex" is a vertex of the graph
- false, otherwise
Exceptions:
--
"""

def get_edges_number(self):
"""
Description:
- returns the number of edges
Input:
--
Output:
- the number of edges
Exceptions:
--
"""

def get_vertices_number(self):
"""
Description:
- returns the number of vertices
Input:
--
Output:
- the number of vertices
Exceptions:
--
"""

def get_in_degree(self, vertex):


"""
Description:
- returns the in degree of a given vertex
Input:
- vertex - the vertex we compute the in degree for
Output:
- the in degree of the wanted vertex
- if the vertex does not exist, the output will be 0
"""

def get_out_degree(self, vertex):


"""
Description:
- returns the out degree of a given vertex
Input:
- vertex - the vertex we compute the out degree for
Output:
- the out degree of the wanted vertex
"""

def get_information_edge(self, x, y):


"""
Description:
- returns the information a given edge stores
Input:
- x, y the source and destination vertices
Output:
- the cost of the edge (x, y)
"""

def modify_information_edge(self, x, y, information):


"""
Description:
- modifies the information of a given edge
Input:
- x, y the source and destination vertices
- information - the new information
Output:
--
"""

def iterate_vertices(self):
"""
Description:
- returns an iterator containing all the vertices
Input:
--
Output:
- the iterator containing all the vertices
"""

def iterate_outbound_edges(self, vertex):


"""
Description:
- returns an iterator containing the outbound edges of a given vertex
Input:
- vertex - the vertex we return the outbound edges for
Output:
- the iterator containing the outbound edges
"""

def iterate_inbound_edges(self, vertex):


"""
Description:
- returns an iterator containing the inbound edges of a given vertex
Input:
- vertex - the vertex we return the inbound edges for
Output:
- the iterator containing the inbound edges
"""

def is_edge(self, x, y):


"""
Description:
- checks if an edge exists
Input:
- x, y - the source and destination vertices
Output:
- true - if the edge exists
- false - if the edge does not exist
"""

def add_edge(self, x, y, information=None):


"""
Description:
- adds an edge to the graph
Input:
- x, y - the source and destination vertices
- information - the cost of the edge
Output:
--
Exceptions:
- raises ValueError exception if the edge already exists
"""

def remove_edge(self, x, y):


"""
Description:
- removes an edge from the graph
Input:
- x, y - the source and destination vertices
Output:
--
Exceptions:
- raises ValueError exception is the edge does not exist
"""

def add_vertex(self, vertex):


"""
Description:
- adds a vertex to the graph
Input:
- the vertex id
Output:
--
Exceptions:
- raises ValueError exception if the vertex already exists
"""
def remove_vertex(self, vertex):
"""
Description:
- removes a vertex from the graph along with its connections
Input:
- the vertex id
Output:
--
Exceptions:
- raises ValueError exception if the vertex does not exist
"""

def get_copy(self):
"""
Description:
- returns a deep copy of the graph
Input:
--
Output:
- a deep copy of the graph
Exceptions:
--
"""

def write_to_file(self, file_name):


"""
Description:
- writes the graph to a file
Input:
- fileName - the name of the file
Output:
--
Exceptions:
- IO exception - if something goes wrong with the writing
"""
class Main():

@staticmethod
def read_from_file(file_name):
"""
Description:
- reads a directed graph from a file and returns it
Input:
- fileName - the name of the file
Output:
- the graph that has been read from the file
"""

@staticmethod
def create_random_graph(number_vertices, number_edges, file_name='random.txt'):
"""
Description:
- creates a random graph with a given number of edges and vertices, and
writes it to a file
Input:
- numberOfVertices - the number of vertices
- numberOfEdges - the number of edges
- fileName - the name of the file the generated graph will be written
Output:
--
Exceptions:
- throws invalidArgument exception if the number of edges >
numberOfVertices*(numberOfVertices-1)

"""

You might also like