You are on page 1of 27

Look at this

Graphs
Everytime I do it makes me Laugh
WMSU

WESTERN MINDANAO STATE UNIVERSITY


What is a graph?
• A data structure that consists of a set of nodes (vertices)
and a set of edges that relate the nodes to each other
• The set of edges describes relationships between 2
vertices
What is a graph?
• A graph G is defined as
follows:
• G = (V,E) where:
– V(G): a finite, nonempty set
of vertices
– E(G): a set of edges (pairs
of vertices)
Types of Graphs
1) Undirected Graph
2) Directed Graph (Digraph)
3) Weighted Graph
4) Cyclic Graph
5) Acyclic Graph
6) Tree
(1) Directed vs. (2) undirected graphs
• When the edges in a
Graph1
graph have no direction,
the graph is called
undirected
(1) Directed vs. (2) undirected graphs
• When the edges in a
graph have a
direction, the graph is
called directed (or
digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !! E(Graph2) = {(1,3), (3,1), (5,7), (5,9), (9,11
(3) Weighted Graph
• Weighted graph: a graph in which each edge carries a
value
(4) Cyclic Graph
• A cyclic graph contains at
least one cycle, which is a
path that starts and ends at
the same node.
• You can traverse the graph
and return to a previously
visited node by following
the edges.
(5) Acyclic Graph
• An acyclic graph does not
contain any cycles.
• This type of graph is often
used in scenarios where a
cycle would inefficient, such as
representing dependencies
between tasks or events.
(6) Trees vs graphs
• Trees are
special cases
of graphs
Graph Terminology
• Adjacent nodes: two nodes are adjacent if they are
connected by an edge
5 is adjacent to 7
7 is adjacent from 5

• Path: a sequence of vertices that connect two nodes in a


graph
Graph Terminology

• Complete graph: a
graph in which every
vertex is directly
connected to every
other vertex
Directed Graph Big O
• What is the number of edges
in a complete directed graph
with N vertices?

Vertices = N
Edges = N * (N-1)
=N*N–N*1
= N2 – N * 1
= O(N2)
Undirected Graph Big O
• What is the number of edges in a complete
undirected graph with N vertices?

Vertices = N
Edges = N * (N-1)
2
= (N * N – N * 1) / 2
= (N2 – N * 1) / 2
= O(N2)
Python Implementation of Graphs
• Array-based implementation
– A 1D array is used to represent
the vertices
– A 2D array (adjacency matrix)
is used to represent the edges
Python Implementation of Graphs
• Vertices and Indices can both be represented by 2D
Arrays, called an Adjacency Matrix
Python Implementation of Graphs
• Alternatively, Graphs in Python can be represented as a
dict of sets
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
Python Implementation of Graphs
• Creating a Graph Object in Python:
Python Implementation of Graphs
• Adding Vertices to Graphs
Python Implementation of Graphs
• Adding Edges between 2 Vertices (Undirected)
Python Implementation of Graphs
• Printing Edges between Vertices
Breadth First Search
Breadth First Search

•• Steps
Step
Step 6:7:5:
Step1:
Step2: Remove
Remove
3:
4: Remove
Initially
Push node
node
node34node
queue from
from0 intothe
the
and1
2 front
0front
fromofofqueue
queue queue
the
visited and and
and
front
arrays
markvisit
visit
ofarethe
the
queueunvisited
it unvisited
and visit
visited.
empty.
neighbours and push them into queue.
theweunvisited
As can see thatneighbours
every neighbours and push
of node 4 isthem
3 are into
visited,
visited, queue.
sosomove
move totothe
thenext
next
node that is
areininthe
thefront
frontofofthe
thequeue.
queue.
Breadth First Search
Depth First Search
Depth First Search

• Step
Step
Step1:2:
Step4:
3:
5: Visit
6:Now,
Now,Node
Initially 0Node
and 1
stack
2put
43 its
atand
atthe adjacent
thevisited
top thenodes
topofofthe stack,
arrays which
so
areso
stack, visitare
empty.
visit not
node
node 1
243
and pop
popyet
visited
and itit from
from the
into the stack
thestack
stack.andand put
put all
all of
of its
its adjacent
adjacent nodes
nodes
which are
which are not not visited
visited (i.e,
in
in the
the3,stack.
stack.
4) in the stack.
Depth First Search

You might also like