Skip to main content

Graphs

A graph is an irreflexive and symmetric relation.

  • A graph GG is a pair (V,E)(V,E)
  • The set VV is the vertices or nodes of GG
  • The set EV×VE \subseteq V \times V is the edges of GG
  • For every (u,v)E(u,v) \in E we have (v,u)E(v,u) \in E - symmetric
  • There are no edges (v,v)(v,v) - irreflexive

This type of graph is called loop-free undirected graph, referring to irreflexive and symmetric.

Graph Visualization

We usually visualize graphs as a set of points (vertices) connected by line (edges).

graph

Graph Definitions

Neighbors

If (u,v)E(u,v) \in E then we say uu and vv are adjacent. We also say that uu and vv are neighbors.

tip

If there is an edge between two vertices then they are adjacent.

The neighborhood of a vertex uu is the set of usu's neighbors.

NG(u)={vV:(u,v)E}N_G(u) = \{v \in V: (u,v) \in E\}

The neighborhood of a set of vertices SVS \subseteq V is a set that contains all neighbors of vertices in SS.

NG(S)={vV:(u,v)EuS}N_G(S) = \{v \in V: (u,v) \in E \land u \in S \}
def N(G, u):
"""
Get the neighbors of a vertex.
G: a graph (V, E)
u: a vertex
"""
V, E = G
return {v for v in V if (u, v) in E}

def NS(G, S):
"""
Get the neighbors of a set of vertices.
G: a graph (V, E)
S: a set of vertices
"""
neighbors = set()
for u in S:
neighbors = neighbors.union(N(G, u))
return neighbors

def NS_2(G, S):
V, E = G
return {v for v in V for u in S if (u,v) in E}
# the graph from the visualization section
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")
}
G = (V, E)

print("Vertices:", V)
print("Edges:", E)
print("Neighbors of A:", N(G, "A"))
print("Neighbors of {A, D}:", NS(G, {"A","D"}))

"""
Vertices: {'D', 'C', 'E', 'A', 'B'}
Edges: {('C', 'B'), ('E', 'D'), ('C', 'A'), ('D', 'E'), ('B', 'A'), ('B', 'C'), ('D', 'B'), ('B', 'D'), ('A', 'C'), ('A', 'B')}
Neighbors of A: {'B', 'C'}
Neighbors of {A, D}: {'B', 'C', 'E'}
"""

Degrees

The degree of a vertex uu is the size of its neighbors.

d(u)=NG(u)d(u) = |N_G(u)|
info

Every edge (u,v)(u,v) adds 1 to the degree of uu and the degree of vv, so we have the sum of degrees:

2E=uVd(u)2|E| = \sum_{u \in V} d(u)
note

We count (u,v)(u,v) and (v,u)(v,u) together as a single edge.

Incident

If e=(u,v)Ee=(u,v) \in E, we say ee is incident with u (and v).

Graph Examples

  • The set of computers in a network, where uu and vv are adjacent if they are connected physically (Ethernet, wifi).
  • The set of cities in the world, uu and vv are adjacent if there is a direct flight.

Handshaking Lemma

Since the sum of degrees is even, there must be an even number of vertices with odd degree.

References