

Self.vert_dict.add_neighbor(self.vert_dict, cost) Self.num_vertices = self.num_vertices + 1 Return str(self.id) + ' adjacent: ' + str()ĭef add_neighbor(self, neighbor, weight=0): In the code, we create two classes: Graph, which holds the master list of vertices, and Vertex, which represents each vertex in the graph: A directed graph with no cycles is called a directed acyclic graph or a DAG. A graph with no cycles is called an acyclic graph.

$$E = \$.Ī cycle in a directed graph is a path that starts and ends at the same vertex. The picture above shows a simple weighted graph and we can represent this graph as the set of six vertices A subgraph $s$ is a set of edges $e$ and vertices $v$ such that $e \in E$ and $v \in V$. We can add a third component to the edge tuple to represent a weight.

Each edge is a tuple $(v,w)$ where $w,v \in V$. $V$ is a set of vertices and $E$ is a set of edges. For example in a graph of roads that connect one city to another, the weight on the edge might represent the distance between the two cities or traffic status.Ī graph can be represented by $G$ where $G=(V,E)$. The picture shown above is not a digraph.Įdges may be weighted to show that there is a cost to go from one vertex to another. If the edges in a graph are all one-way, the graph is a directed graph, or a digraph. A vertex may also have additional information and we'll call it as payload.Īn edge is another basic part of a graph, and it connects two vertices/ Edges may be one-way or two-way. A vertex is the most basic part of a graph and it is also called a node.
