Graph Definitions
Representation
Applications
Definitions
• A graph G = ( V, E ) consists of a set V of vertices (or
nodes), and a set E of edges that connect the vertices.
• Each edge in E is a pair ( u, v ) of vertices from V.
• In a directed graph, edges are ordered pairs (u,v) – in
other words the order of vertices in the pair matters,
hence (u,v) and (v,u) are two different edges.
• In an undirected graph, edges are unordered pairs
(u,v) – the order of vertices in the pair does not matter.
Definitions
Undirected & Directed Graphs
Definitions
• Vertex v is said to be adjacent to u if (u,v) is an
edge.
• A path in a graph G, is a sequence of vertices
connected by edges, in other words, it is a
sequence v1, v2, … , vN such that every ( vi, vi+1 ) is
an edge.
• The length (or the unweighted length) of the
path is the number of edges in the sequence. The
above path has length N-1.
Definitions
• A path with no edges, hence with only one
vertex, has length 0.
• A simple path is a path where all vertices are
distinct, except that the first and the last can be
identical.
• A cycle is a path that begins and ends at the same
vertex, and contains at least one edge.
Definitions
• Directed Acyclic Graphs ( DAG )– graphs of no
cycles, if the edges are directed, are said to be
DAGs.
A undirected cycle No cycle
A DAG
Definitions
• Each edge in a graph can be assigned a cost or a
weight.
• Then the weight (or the weighted length) of a
path is the sum of the weights of its edges.
Weighted Graphs
Applications
• The airport system:
• Each airport is a vertex.
• A nonstop flight between two airports is modeled
by an edge.
• Edge weight – cost of a flight or distance between
two airports.
Applications
• Problem:
• Find best flight between two airports
• Best flight could mean:
– with fewest stops
– of smallest distance
– of lowest cost
• Shortest path problem
Complete Graphs
• Complete graph ที่มี v vertices มี v(v – 1)/2 edges
Connected Graphs
• A connected (undirected) graph with v vertices
has at least v – 1 edges
• A simple graph with v vertices and C(v – 1, 2)
edges must be connected
Degree
• e2 is incident on v2
• v1 is adjacent to v2
• Degree of v3 is 3
Subgraphs
• A subgraph is a subset of a graph's edges (and
associated vertices) that constitutes a graph
Representations of Graphs
• Each vertex can be identified with a number
between 1 and N.
• How to represent edges?
1. Adjacency matrix
– A matrix A of N × N entries
– Aij is 1 if ( i, j ) is in E (and zero otherwise)
– If edges have weights matrix A can store the edge
weights.
Indegree and Outdegree
• Indegree = # of edges • Outdegree = # of edges
into a vertex. leaving a vertex.
Adjacency List
2. An adjacency list
– For each vertex keep a list of its adjacent vertices.
– Reduces the space usage to O( |E| + |V| )
– It is a more efficient representation in case of
sparse graphs.
Adjacency List
Graph Representations