You are on page 1of 23

GRAPH DATA

STRUCTURE
• GRAPH TYPES
• HOW IT WOKS
• TRAVERSAL TECHNIQUES
DEFINITION
• A Graph is a non-linear data structure consisting of nodes which are
connected to each other through edges.
• The nodes are sometimes also referred to as vertices and the edges are
lines or arcs that connect any two nodes in the graph.
• A Graph consists of a finite set of vertices (or nodes) and set of Edges
which connect a pair of nodes
nodes

GRAPH OUTLINE
Example:
TERMS
• Node or vertex- Fundamental unit from which graphs are formed usually
represented by a dot on the graph
• Edge- This is a connection between two vertices.
• Degree - this is the number of neighboring nodes for each node.
• Adjacency- This refers to the connection(s) between a node and its
neighbor
• Path – a sequence of vertices in a graph and a graph is connected if there
is a path from every vertex.
•every vertex

PATH
• The path is marked in red
CYCLE
• a path in which the source vertex is also the destination vertex
PROPERTIES OF GRAPHS
Primarily, graphs are classified into three properties namely:
• Weighted/unweighted
• Directed/undirected
• Cyclic/acyclic
DIRECTED GRAPH
• Also called a digraph, it is a graph in which the edges are presented in one
direction
• The arrows are mono-directed that is a connection from A to B is not
necessarily reversed unless there is an arrow indicating the reverse
direction.
UNDIRECTED GRAPH
• The graph is bidirectional that is the edges indicate a two-
way relationship, in that each edge can be traversed in both directions (It
can go in forward and backward directions)
WEIGHTED GRAPH
• Is a graph with edges labeled by numbers (called weights)
• The weight is sometimes referred to as cost representing speed limit,
diameter of pipe, time or distance depending with the application of the
graph.
CYCLIC/ACYCLIC GRAPHS
Cyclic graph:
• An undirected graph is by default a cyclic graph because movement is
possible in any direction so the source node can be the destination node.
• A cycle graph is one that contains complete cycle.
Acyclic graph:
• Acyclic graph has no cycles within the graph and this is for a directed
graph in which the source node is not by any chance the destination node.
GRAPH IMPLEMENTATION
• Adjacency List
• Adjacency matrix
• Edge list
ADJACENCY LIST
• An adjacency list is a collection of unordered lists used to represent a
finite graph.
• Each vertex stores the list of neighbouring nodes in a linked list or array.
• Adjacency lists are faster and space efficient for sparse graphs and very
slow for dense graphs and a tuple would have to be use for dense graphs.
• For a weighted graph the, the adjacency list includes the weights of the
neighbouring nodes.
ADJACENCY LIST ILLUSTRATION
• The adjacency lists shows the neighboring vertices and their weights
ADJACENCY MATRIX
• It is a 2-D array (an n by n matrix where n is the number of nodes) that
indicates the number of edges in the graph.
• The connection between the nodes is represented by ones for an
unweighted graph and the weight for a weighted graph and zeros or blank
spaces indicate that there is no connection between two nodes.
• The vertical axis represents the ‘from’ direction and the horizontal node
represent the ‘to’ direction of the graph so the adj[A][B] indicates an edge
from A to B.
ADJACENCY MATRIX ILLUSTRATION

• The adjacency matrix contains the weights of the graph


EDGE LIST
• An edge list is at most a vector that contains all edges as lists of all paired
nodes and the weight included for a weighted graph.
• It can change into a more programmable form which is an adjacency list
or an adjacency matrix. For an undirected graph each edge is added in
both directions i.e if in the edge list there is (A,B) then in the adjacency
list, A should appear in B and B in A.
TYPES OF GRAPHS
Graphs are grouped according to their relative numbers of edges and vertices.
Dense graph:
• A graph with more edges than vertices that is;|number of edges|= |number of vertices|2
• Adjacency matrix are more efficiency for dense graphs because it always represents |number of
vertices|2 despite the type of the graph.
Sparse graph:
• A graph with relatively equal number of edge and vertices or more vertices than edges.
• An adjacency is more relevant for a sparse graph because its saves space and only store the
necessary information.
GRAPH TRAVERSAL METHODS
• A traversal method is a way of visiting all nodes that can be reached from
the given starting node.
• A graph has two fundamental traversal methods whose difference in the
order in which they visit the nodes namely
• Depth-first search(DFS)
• Breadth-first search(BFS)
DEPTH-FIRST METHOD
• This is a straight forward traversal technique in which the algorithm starts from the stated
starting node, takes the direction of one of the neighbors and proceed to the all other
reachable nodes in one direction as deep as possible until there are no more possible edges
to follow, then back to the previous node and trace the viable nodes in one direction until
all possibilities are exploited.
• The algorithm keeps track of the visited nodes to ensure one visit on each node, thus its
time complexity is O(n + m) where n is the number of nodes and m for edges.
• This traversal method makes use of the stack data structure that is each new node is added
to the top and removal of the nodes is from the top (Last In First Out) and is conveniently
implemented using recursion.
DEPTH-FIRST SEARCH ILLUSTRATION
BREADTH-FIRST SEARCH

• This technique visits nodes in increasing order of their distance from the starting node meaning it explores the
neighboring nodes first.
• Is visits the nodes one level after another, meaning it firstly explores nodes whose distance from the starting
node is 1 then goes to those whose distance is two which are the neighbors of the neighbors until all the
vertices are visited.
• Like in depth-first search, the algorithm also keeps track of the visited nodes to make sure each node is
visited once.
• On the other hand, the breadth-first search can be used to calculate the distance of the other nodes form the
starting nodes, hence the time complexity is the same as that of the depth-first search: O( n + m) with n as the
number of nodes and m as the number of edges.
• Unlike the depth-first search, the breadth-first search is more difficult to implement because the algorithm
visits nodes in different parts of the graph at the same time, as a result the technique makes use of queues
where the nodes are added to the back of the queue and removed/pushed from the top of the queue (FIFO).
BREADTH-FIRST SEARCH ILLUSTRATION

• The queue contains nodes to be processed in increasing order of their


distance and new nods are always added to the end of the queue with the
node at the beginning of the queue as the next to be processed.

You might also like