Skip to content

Commit 8bafa14

Browse files
committed
Three Hundred - Twenty-Six Commit: Create Graph Class
1 parent c1c44b6 commit 8bafa14

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Section_12(Graphs)/(1)_graph.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Graph (Abstract Data Type)
2+
# A graph data structure is a collection of nodes that have data and are connected to other nodes.
3+
# A graph data structure consists of a finite (and possibly mutable) set of vertices (also called nodes or points),
4+
# together with a set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for a directed graph.
5+
# These pairs are known as edges (also called links or lines), and for a directed graph are also known as edges but also sometimes arrows or arcs.
6+
# The vertices may be part of the graph structure, or may be external entities represented by integer indices or references.
7+
# A graph data structure may also associate to each edge some edge value, such as a symbolic label or a numeric attribute (cost, capacity, length,
8+
# etc.).
9+
#
10+
# More precisely, a graph is a data structure (V, E) that consists of
11+
# - A collection of vertices V
12+
# - A collection of edges E, represented as ordered pairs of vertices (u,v)
13+
#
14+
# Graph Terminology
15+
# Adjacency: A vertex is said to be adjacent to another vertex if there is an edge connecting them. Vertices 2 and 3 are not adjacent because there is
16+
# no edge between them.
17+
# Path: A sequence of edges that allows you to go from vertex A to vertex B is called a path. 0-1, 1-2 and 0-2 are paths from vertex 0 to vertex 2.
18+
# Directed Graph: A graph in which an edge (u,v) doesn't necessarily mean that there is an edge (v, u) as well. The edges in such a graph are represented
19+
# by arrows to show the direction of the edge.
20+
#
21+
# Operations
22+
#
23+
# The basic operations provided by a graph data structure G usually include:[1]
24+
#
25+
# adjacent(G, x, y): tests whether there is an edge from the vertex x to the vertex y;
26+
# neighbors(G, x): lists all vertices y such that there is an edge from the vertex x to the vertex y;
27+
# add_vertex(G, x): adds the vertex x, if it is not there;
28+
# remove_vertex(G, x): removes the vertex x, if it is there;
29+
# add_edge(G, x, y, z): adds the edge z from the vertex x to the vertex y, if it is not there;
30+
# remove_edge(G, x, y): removes the edge from the vertex x to the vertex y, if it is there;
31+
# get_vertex_value(G, x): returns the value associated with the vertex x;
32+
# set_vertex_value(G, x, v): sets the value associated with the vertex x to v.
33+
#
34+
# Structures that associate values to the edges usually also provide:[1]
35+
#
36+
# get_edge_value(G, x, y): returns the value associated with the edge (x, y);
37+
# set_edge_value(G, x, y, v): sets the value associated with the edge (x, y) to v.
38+
#
39+
# Graph ADT implemented in Python using the Adjacency List Graph Representation
40+
# An adjacency list represents a graph as an array of linked lists.
41+
# The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex.
42+
# Text Source: Graph Data Structure
43+
# URL: https://www.programiz.com/dsa/graph
44+
# Text Source: Graph (abstract data type)
45+
# URL: https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
46+
47+
import numpy as np
48+
49+
class Graph:
50+
def __init__(self, vertices):
51+
self._vertices = vertices
52+
self._adjacent_matrix = np.array((vertices, vertices))

0 commit comments

Comments
 (0)