Professional Documents
Culture Documents
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
• 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