ADA Lect4
ADA Lect4
Analysis
Lecture 4: Graphs
Basic Definitions
• A graph G is simply a way of encoding pairwise relationships among a set of objects.
• It consists of a collection V of nodes and a collection E of edges, each of which “joins”
two of the nodes.
• Thus, an edge e ∈ E is represented as a two-element subset of V: e = {u, v} for some u, v
∈V
• where u and v are called the ends of e.
• Edges in a graph indicate a symmetric relationship between their ends.
• Often asymmetric relationships is to be encoded, and for this closely related notion of
a directed graph (digraph) is used.
• A directed graph G consists of a set of nodes V and a set of directed edges E.
• Each e ∈ E is an ordered pair (u, v)
• the roles of u and v are not interchangeable
• u is called the tail of the edge and v the head.
Basic Definitions (2)
The graph G = ({1, 2, 3, 4, 5}, {{1, 2}, G = ({1, 2, 3, 4, 5}, {(1, 2), (1, 3), (2, 1),
{1, 3}, {2, 3}, {2, 4}, {3, 5}, {4, 5}}) (3, 2), (4, 3), (4, 5), (5, 2), (5,4)})
Applications
• Transportation networks.
• Communication networks.
Paths and Connectivity
• A weighted graph or digraph is one where each edge has a value, called the weight,
associated with it.
• The weight is considered to be the cost for traversing the edge.
• A path through a weighted graph has a cost that is the sum of the weights of each edge in the path.
• The shortest path between two nodes is the path with the smallest cost, even if it doesn’t have the fewest
edges.
• A graph or digraph is called connected if there is at least one path between every pair of
nodes.
• A cycle is a path that begins and ends at the same node.
• An acyclic graph or digraph is one that has no cycles.
DATA STRUCTURE METHODS FOR GRAPHS
• There are two ways that we can store the graph or digraph information:
• adjacency matrix
• an adjacency list.
• There is no difference between how these methods are used for graphs and digraphs.
• These storage methods will also neutralize the differences between graphs and digraphs,
and so the algorithms that use these structures will not need to differentiate between
graphs and digraphs either.
• An adjacency matrix gives us the ability to quickly access edge information
• If the graph is far from being a complete graph, there will be many more empty elements in the array
than there are full elements.
• An adjacency list uses space that is proportional to the number of edges in the graph
• The time to access edge information may be greater.
DATA STRUCTURE METHODS FOR GRAPHS
• An adjacency matrix, AdjMat, for a graph G = (V, E), with |V| = N, will be stored as a two
dimensional array of size NX N.
• Each location [i, j] of this array will store a 0, except if there is an edge from node vi to node
vj, the location will store a 1.
• For weighted graphs and digraphs, the adjacency matrix entries would be ∞ if there is no edge and the
weight for the edge in all other cases.
• The diagonal elements would be 0, because there is no cost to travel from a node to itself.
DATA STRUCTURE METHODS FOR GRAPHS:
Adjacency Matrix
DATA STRUCTURE METHODS FOR GRAPHS:
Adjacency List
• An adjacency list, AdjList, for a graph G = (V, E), with |V| = N, will be stored as a one-
dimensional array of size N,
• with each location being a pointer to a linked list.
• There will be one list for each node, and that list will have one entry for each adjacent node
• For weighted graphs and digraphs, the adjacency list entries would have an additional field
to hold the weight for that edge.
DATA STRUCTURE METHODS FOR GRAPHS:
Adjacency List
TRAVERSAL ALGORITHMS
• When we work with graphs, there may be times that we wish to do something to each
node in the graph exactly once.
• For example, distribute a piece of information all of the computers on a network.
• The same thing would be true if we were looking for information instead of distributing it.
• There are two techniques that we will examine that accomplish this traversal.
• In depth-first, our traversal will go as far as possible down a path before considering
another
• In breadth-first, our traversal will go evenly in many directions.
• For these two traversal methods, one node is chosen as our starting point.
• The phrase “visit the node” represents the action that needs to be done at each node.
• Note that these methods work with both directed and undirected graphs without any
changes.
TRAVERSAL ALGORITHMS
• In depth-first traversal, we visit the starting node and then proceed to follow
links through the graph until we reach a dead end.
• In an undirected graph, a node is a dead end if all of the nodes adjacent to it
have already been visited.
• In a directed graph, if a node has no outgoing edges, we also have a dead end.
• When we reach a dead end, we back up along our path until we find an unvisited
adjacent node and then continue in that new direction.
• The process will have completed when we back up to the starting node and all
the nodes adjacent to it have been visited.
• If presented with a choice of two nodes, we will choose the node with the
numerically or alphabetically smaller label.
• When this algorithm is implemented, that choice will depend on how the edges of the graph
are stored.
TRAVERSAL ALGORITHMS : DEPTH-FIRST