Graph Connectivity
Path
A path is a sequences of vertices such that:
- No vertex appears more than once.
- is adjacent to . for each .
- The length of the path is which is the number of edges.
- is a path of length 3.
- is a path of length 1.
- is a path of length 2.
Cycle
A cycle is just like a path, but , so that it loops back on itself.
- The length of a cycle is , which is the number of edges, and also the number of vertices.
- We don't care about the starting vertex of a cycle. So is the same cycle as .
- We might not use the above notation (duplicate the start/end vertex) to represent a cycle. Instead we will use "cycle ". Note: this is different from path, path can be represented by "path ".
- is a cycle of length 3.
Connectedness
If there is a path from to for every , then we say that is connected. Otherwise, we say is disconnected.
The graph is connected.
Connected Component
If for every there is a path between and , and there are no paths to any vertices outside .
Then we call is a connected component of .
The graph is disconnected.
The connected components are and
Distance
The distance between vertices and is the length of a shortest path from to .
Calculating Distances
Given a fixed vertex , we define:
- to be the set of vertices that is distance from . (explored)
- to contain the set of vertices that has distance at least from . (unexplored)
The visualization of the graph can be found here.
def distance_classes(V:set, E:set, u):
"""
V: a set of vertices.
E: a set of edges.
u: the starting vertex.
return: A list D. The vertices in D[1] is 1 distance from u.
"""
D = [{u}]
V_unexplored = V - {u}
return distance_classesR(V_unexplored, E, D)
def distance_classesR(V,E, D):
V_unexplored = V - D[-1]
if len(V_unexplored) == 0:
return D
D_new = NS(V_unexplored, E, D[-1])
D = D + [D_new]
return distance_classesR(V_unexplored, E, D)
def NS(V,E,S):
return {v for u in S for v in V if (u,v) in E}
V = {"A", "B", "C", "D", "E"}
E = {
("A","B"),("B","A"),
("A","C"),("C","A"),
("B","C"),("C","B"),
("B","D"),("D","B"),
("D","E"),("E","D")
}
print(distance_classes(V,E, "A"))
# [{'A'}, {'C', 'B'}, {'D'}, {'E'}]
Bipartite Graph
If we are able to separate/partition vertices into two sets such that vertices within each set are not adjacent, then we call a bipartition of .
A graph is bipartite iff has no cycles of odd length.
What is a Bipartite Graph? | Graph Theory
Vocabulary
Cannot find definitions for "bipartite".