Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ A Python Class
- A simple Python graph class. https://repl.it/GyUb/0
- """
- class Graph(object):
- def __init__(self, graph_dict=None):
- """ initializes a graph object
- If no dictionary or None is given,
- an empty dictionary will be used
- """
- if graph_dict == None:
- graph_dict = {}
- self.__graph_dict = graph_dict
- def vertices(self):
- """ returns the vertices of a graph """
- return list(self.__graph_dict.keys())
- def edges(self):
- """ returns the edges of a graph """
- return self.__generate_edges()
- def add_vertex(self, vertex):
- """ If the vertex "vertex" is not in
- self.__graph_dict, a key "vertex" with an empty
- list as a value is added to the dictionary.
- Otherwise nothing has to be done.
- """
- if vertex not in self.__graph_dict:
- self.__graph_dict[vertex] = []
- def add_edge(self, edge):
- """ assumes that edge is of type set, tuple or list;
- between two vertices can be multiple edges!
- """
- edge = set(edge)
- (vertex1, vertex2) = tuple(edge)
- if vertex1 in self.__graph_dict:
- self.__graph_dict[vertex1].append(vertex2)
- else:
- self.__graph_dict[vertex1] = [vertex2]
- def __generate_edges(self):
- """ A static method generating the edges of the
- graph "graph". Edges are represented as sets
- with one (a loop back to the vertex) or two
- vertices
- """
- edges = []
- for vertex in self.__graph_dict:
- for neighbour in self.__graph_dict[vertex]:
- if {neighbour, vertex} not in edges:
- edges.append({vertex, neighbour})
- return edges
- def __str__(self):
- res = "vertices: "
- for k in self.__graph_dict:
- res += str(k) + " "
- res += "\nedges: "
- for edge in self.__generate_edges():
- res += str(edge) + " "
- return res
- if __name__ == "__main__":
- g = { "a" : ["d"],
- "b" : ["c"],
- "c" : ["b", "c", "d", "e"],
- "d" : ["a", "c"],
- "e" : ["c"],
- "f" : []
- }
- graph = Graph(g)
- print("Vertices of graph:")
- print(graph.vertices())
- print("Edges of graph:")
- print(graph.edges())
- print("Add vertex:")
- graph.add_vertex("z")
- print("Vertices of graph:")
- print(graph.vertices())
- print("Add an edge:")
- graph.add_edge({"a","z"})
- print("Vertices of graph:")
- print(graph.vertices())
- print("Edges of graph:")
- print(graph.edges())
- print('Adding an edge {"x","y"} with new vertices:')
- graph.add_edge({"x","y"})
- print("Vertices of graph:")
- print(graph.vertices())
- print("Edges of graph:")
- print(graph.edges())
- def dfs(graph, start):
- visited, stack = set(), [start]
- while stack:
- vertex = stack.pop()
- if vertex not in visited:
- visited.add(vertex)
- stack.extend(set(graph.graphdict[vertex]) - visited)
- return visited
- If you start this module standalone, you will get the following result:
- $ python3 graph.py
- Vertices of graph:
- ['a', 'c', 'b', 'e', 'd', 'f']
- Edges of graph:
- [{'a', 'd'}, {'c', 'b'}, {'c'}, {'c', 'd'}, {'c', 'e'}]
- Add vertex:
- Vertices of graph:
- ['a', 'c', 'b', 'e', 'd', 'f', 'z']
- Add an edge:
- Vertices of graph:
- ['a', 'c', 'b', 'e', 'd', 'f', 'z']
- Edges of graph:
- [{'a', 'd'}, {'c', 'b'}, {'c'}, {'c', 'd'}, {'c', 'e'}, {'a', 'z'}]
- Adding an edge {"x","y"} with new vertices:
- Vertices of graph:
- ['a', 'c', 'b', 'e', 'd', 'f', 'y', 'z']
- Edges of graph:
- [{'a', 'd'}, {'c', 'b'}, {'c'}, {'c', 'd'}, {'c', 'e'}, {'a', 'z'}, {'y', 'x'}]
Advertisement
Add Comment
Please, Sign In to add comment