Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

GRAPHS

21
Basic concepts
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
 An undirected graph is one in which the pair of vertices in a edge
is unordered, (v0, v1) = (v1,v0)
 A directed graph is one in which each edge is a directed pair of
vertices, <v0, v1> != <v1,v0>
Definition: Directed graph
• Directed Graph
– A directed graph, or digraph, is a pair
– G = (V, E)
– where V is a set whose elements are called vertices, and
– E is a set of ordered pairs of elements of V.

• Vertices are often also called nodes.


• Elements of E are called edges, or directed edges, or arcs.
• For directed edge (v, w) in E, v is its tail and w its head;
• (v, w) is represented in the diagrams as the arrow, v -> w.
• In text we simple write vw.
Definition: Undirected graph
• Undirected Graph
– A undirected graph is a pair
– G = (V, E)
– where V is a set whose elements are called vertices, and
– E is a set of unordered pairs of distinct elements of V.
• Vertices are often also called nodes.
• Elements of E are called edges, or undirected edges.
• Each edge may be considered as a subset of V containing
two elements,
• {v, w} denotes an undirected edge
• In diagrams this edge is the line v---w.
• In text we simple write vw, or wv
• vw is said to be incident upon the vertices v and w
Definitions: Weighted Graph
• A weighted graph is a triple (V, E, W)
– where (V, E) is a graph (directed or undirected) and
– W is a function from E into R, the reals (integer or rationals).
– For an edge e, W(e) is called the weight of e.
More Definitions
• Subgraph
• Symmetric digraph
• complete graph- a graph in which every vertex is directly connected to
every other vertex
• Adjacency relation
• Path- a sequence of vertices that connect two nodes in a graph
• Connected, Strongly Connected
• Cycle, simple cycle
• Acyclic - An acyclic graph is a graph having no graph cycles.
• forest -A forest is an undirected graph in which any two vertices are
connected by at most one path. Equivalently, a forest is an undirected
acyclic graph.
• free tree, undirected tree
• rooted tree
• Connected component
Paths and Cycles
A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1)  E for all
0  i < n.
A cycle is a path that begins and ends at the same node.

Chicago
Seattle

Salt Lake City

San Francisco
Dallas
p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}
Path Length and Cost
Path length: the number of edges in the path
Path cost: the sum of the costs of each edge

3.5 Chicago
Seattle
2 2

2 Salt Lake City


2.5
2.5 2.5

3
San Francisco
Dallas
length(p) = 5 cost(p) = 11.5
Connectivity
Undirected graphs are connected if there is a path between any two
vertices

Directed graphs are strongly connected if there is a path from any one
vertex to any other

Directed graphs are weakly connected if there is a path between any


two vertices, ignoring direction

A complete graph has an edge between every pair of vertices


Complete graph
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum number of
edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph
Trees as Graphs

• Every tree is a graph with some A


restrictions:
– the tree is directed
B C
– there are no cycles (directed or
undirected)
– there is a directed path from D E F
the root to every node
BAD! G H

I J
Directed Acyclic Graphs (DAGs)

DAGs are directed graphs


with no cycles. main()

mult()
if program call
graph is a DAG, then add()
all procedure calls
can be in-lined
read()
access()

Trees  DAGs  Graphs


Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to represent the
edges
• Linked-list implementation
– A 1D array is used to represent the vertices
– A list is used for each vertex v which contains the vertices
which are adjacent from v (adjacency list)
Graph Representations
• Adjacency Matrix Representation
– Let G = (V,E), n = |V|, m = |E|, V = {v1, v2, …, vn)
– G can be represented by an n  n matrix
Adjacency Matrix for weight digraph
Array of Adjacency Lists
Array of Adjacency Lists

from -> to, weight


Applications of graphs
• In Computer science graphs are used to represent the flow of
computation.
• Google maps uses graphs for building transportation systems,
where intersection of two(or more) roads are considered to be a
vertex and the road connecting two vertices is considered to be an
edge, thus their navigation system is based on the algorithm to
calculate the shortest path between two vertices.
• In Facebook, users are considered to be the vertices and if they
are friends then there is an edge running between them.
Facebook’s Friend suggestion algorithm uses graph theory.
• In Operating System, we come across the Resource Allocation
Graph where each process and resources are considered to be
vertices. Edges are drawn from resources to the allocated process,
or from requesting process to the requested resource. If this leads
to any formation of a cycle then a deadlock will occur.

You might also like