### Introduction to Graphs

• Graph

• Graph:

• A graph consists of:

 a set of vertices (or nodes) V a list (collection) of edges (or links) E between two vertices of the graph

• Example:

 Graph G = {V, E} where: V = {A, B, C, D} E = {(A,B), (B,C), (C,D), (A,D)}

Graphical representation:

• Use of graphs

• Graphs are often used to represent relationship information

• Example: "friendship" relationship

• Example: flight database

• Definitions

• Vertex set

 Vertex set V(G) = the set of vertices of graph G I.e., no duplicates

• Edge-list

 Edge-list E(G) = the list (collection) of edges of graph G I.e.: possible duplicates

• Important note on graphs

• Notes:

• The collection of vertices is a set

 There are no duplicate elements in a set

• The collection of edges is a list

 There can be duplicate elements in a list I.e.: the same edge can appear more than once !!!

• Some examples of "extreme" graphs:

• Multiple edges between vertices:

(Each edge represents a way to travel between 2 locations)

• Self-loops: a edge from a node to itself

(An edge represents a relationship between 2 things; a self loop represents the fact that some things are "related to itself".)

• Simple graphs

• Simple graph:

• A graph that does not have:

 Multiple edges between vertices Self-loops

• Property of simple graphs:

 The list of edges is a set (no duplicate elements)

• More terminology: properties of an edge

 The vertices u and v are adjacent is they are connected by an edge in graph G

• Incident edge:

 An edge that connects the vertices u and v is said to be incident with u and v

• Degree of a node:

 Degree of a node v = number of incident edges to node v

Example:

The degree of each node = 2

• More definitions: how edges are connected

• Walk:

• Walk between u and z = a seccession of edges of the form:

 uv, vw, wx, ...., yz

Example:

Walk = (A,B), (B,C), (C,D)

• Trail:

• Trail = a walk where each edge is different

Example:

I.e.: trail = use an edge at most once

• Path:

• Path = a walk where each edge and each vertex is different

Example:

I.e.: path = visit a vertex at most once

• Cycle:

• Cycle = a "path" where starting vertex is equal to the ending vertex

(I put "path" in quotes because the starting vertex is repeated)

Example:

• Complete graph

• Complete graph:

 Complete graph = a graph where every vertex is adjacent to every other vertex Kn = the complete graph containing n vertices

Example:

• Directed and undirected edges

• Edges:

• Edges of a graph can be:

 directed,     or undirected

• Undirected edges:

• Undirected edges is like a two-way street

• You can traverse an undirected edge (u,v) as:

 u ⇒ v,    or as v ⇒ u

• Undirect edges are sometimes denoted as {u,v}

• Direct edges:

• Undirected edges is like a one-way street

• You can only traverse an directed edge (u,v) as:

 u ⇒ v

• Directed graph:

 Directed graph = a graph with directed edges

Example:

• Property of graphs

• Property 1:

• If a (undirected) graph G has m edges then:

 ``` ∑ deg(v) = 2m v ∈ G ```

• Proof:

• Each edge is incident to 2 nodes

• Therefore, each edge contributes 2 to the total sum

• There are m edges

Each edge contributes 2 to the total sum

Therefore: The total sum = 2 × m

Example:

There are 5 edges

And: deg(A) + deg(B) + deg(C) + deg(D) = 3 + 2 + 3 + 2 = 10

• Property 2:

• If a directed graph G has m edges then:

 ``` ∑ indeg(v) = ∑ outdeg(v) = m v ∈ G v ∈ G ```

• Proof:

• Each directed edge is incident to 2 nodes:

directed edge:

 once as an outgoing edge once as an incoming edge

• Therefore, each directed edge contributes:

 1 to the total sum of "indeg(v)" 1 to the total sum of "outdeg(v)"

• There are m edges

Each edge contributes 1 to each of the total sum

Therefore:

• The total sum "indeg(v)" = m
• The total sum "outdeg(v)" = m

Example:

There are 5 edges

And: indeg(A) + indeg(B) + indeg(C) + indeg(D) = 1 + 1 + 2 + 1 = 5
outdeg(A) + outdeg(B) + outdeg(C) + outdeg(D) = 2 + 1 + 1 + 1 = 5

• Property 3:

• Let G be a simple undirected graph with n vertices and m edges

Then:

 ``` n(n-1) m ≤ ------- 2 ```

Proof:

• One edge connects 2 vertices

• There are n different vertices

• The maximum number of ways to select 2 different vertices is (according to combinatorics):

 ``` n! Choose(n, 2) = ----------- (n-2)! 2! n (n-1) (n-2) (n-3).... 1 = --------------------------- [(n-2) (n-3) ... 1] [2] n(n-1) = -------- 2 ```

• Therefore: the maximum number of (distinct) edges in a graph with n vertices = (n × (n-1))/2

Since this number is the maximum, it follows that:

 ``` m = a possible number of edges in a graph ≤ maximum number of edges in a graph n (n-1) ≤ --------- 2 ```

• Example: graphs with the maximum # edges are complete graphs

Kn has (n×(n−1))/2 edges

• Other terminologies

• Subgraph:

• Graph S = {V(S), E(S)} is a subgraph of graph G = {V(G), E(G)} if:

 V(S) ⊆ V(G) E(S) ⊆ E(G)

Example:

• Spanning subgraph:

• Graph S = {V(S), E(S)} is a spanning subgraph of graph G = {V(G), E(G)} if:

 S is a subgraph of G,    and        V(S) = V(G)

Example:

• Connected graph:

• Connected graph = a graph where for every pair of nodes (u,v), there exists a path connecting the nodes u and v

Example:

• Connected component:

• Connected component = a maximally connected subgraph

Example:

• Tree:

• Tree = a connected graph that does not have any cycle

Example:

• Forest:

• Forest = a (not necessaily connected) graph that does not have any cycle

Example:

• Spanning tree:

• Spanning tree = a spanning subgraph that is a tree

Example:

• Some properties of trees and graphs

• # edges in some graphs:

• Let G be a tree with n vertices and m edges

Then:

 ``` m = n -1 ```

• Let G be a forest with n vertices and m edges

Then:

 ``` m ≤ n -1 ```

• Let G be a connected graph with n vertices and m edges

Then:

 ``` m ≥ n -1 ```