You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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.
0 commit comments