0% found this document useful (0 votes)
30 views21 pages

ADA Lect4

The document discusses graphs and graph algorithms. It defines graphs and different types of graphs. It also describes common graph algorithms like breadth-first search and depth-first search which are used to traverse graphs. Data structures like adjacency lists and matrices are also introduced to represent graphs. Finally, it discusses applications of graphs in transportation and communication networks.

Uploaded by

H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views21 pages

ADA Lect4

The document discusses graphs and graph algorithms. It defines graphs and different types of graphs. It also describes common graph algorithms like breadth-first search and depth-first search which are used to traverse graphs. Data structures like adjacency lists and matrices are also introduced to represent graphs. Finally, it discusses applications of graphs in transportation and communication networks.

Uploaded by

H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Algorithm Design and

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)

• By default, the term “graph” will mean an undirected graph.


• It is also worth mentioning
• First, although an edge e in an undirected graph should properly be written as a set of
nodes {u, v}, one will more often see it written in the notation used for ordered pairs: e = (u,
v).
• Second, a node in a graph is also frequently called a vertex; in this context, the two words
have exactly the same meaning.
Basic Definitions (3)

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 complete graph is a graph with an edge between every pair of nodes.


• If there are N nodes, there will be (N2- N) / 2 edges in a complete graph without loop edges.
• A complete digraph is a digraph with an edge allowing traversal between every pair of nodes.
• A digraph with N nodes will have twice as many edges, specifically (N2- N).
• A subgraph (Vs, Es) of a graph or digraph (V, E) is one that has a subset of the vertices (Vs
⊆V) and edges (Es ⊆ E) of the full graph.
• A path between two nodes of a graph or digraph is a sequence of edges that can be traveled
consecutively.
• All of the nodes along this path is required to be unique.
• A path is said to have a length that represents the number of edges that make up the path. The path AB,
BC, CD, DE has length 4
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

• There is no clear benefit to either of these methods.


• The choice between these two will be closely linked to knowledge of the graphs that will be input to the
algorithm.
• In situations where the graph has many nodes, but they are each connected to only a few
other nodes, an adjacency list would be best
• it uses less space,
• and there will not be long edge lists to traverse.
• If the graph has few nodes, an adjacency matrix would be better
• because it would not be very large, so even a sparse graph would not waste many entries.
• In situations where the graph has many edges and begins to approach a complete graph,
• an adjacency matrix would be best because there would be few empty entries.
DATA STRUCTURE METHODS FOR GRAPHS:
Adjacency Matrix

• 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

• Either of these traversal methods can also be used to determine if a graph is


connected.
• If we create a list of the nodes we visit during our traversal, this list can be
compared to the set of nodes in the graph.
• If they are the same, the graph is connected.
• If they are not, there are some nodes that cannot be reached from where we
started, meaning that the graph is not connected.
TRAVERSAL ALGORITHMS : DEPTH-FIRST

• 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

• Consider the opposite graph


• If we begin the depth-first traversal at node 1, we
then visit, in order, the nodes 2, 3, 4, 7, 5, and 6
before we reach a dead end.
• We would then back up to node 7 to find that
node 8 hasn’t been visited, but that immediately
leads to a dead end.
• We next back up to node 4 and find that node 9
hasn’t been visited, but again we have an
immediate dead end.
• We then continue to back up until we reach the
starting node, and because all nodes adjacent to
it have been visited, we are done.
TRAVERSAL ALGORITHMS : DEPTH-FIRST

• Consider the opposite graph


• If we begin the depth-first traversal at node 1, we
then visit, in order, the nodes 2, 3, 4, 7, 5, and 6
before we reach a dead end.
• We would then back up to node 7 to find that
node 8 hasn’t been visited, but that immediately
leads to a dead end.
• We next back up to node 4 and find that node 9
hasn’t been visited, but again we have an
immediate dead end.
• We then continue to back up until we reach the
starting node, and because all nodes adjacent to
it have been visited, we are done.
TRAVERSAL ALGORITHMS : Breadth-First
Traversal
• In a breadth-first traversal, we visit the starting node and then on
the first pass visit all of the nodes directly connected to it.
• In the second pass, we visit nodes that are two edges “away” from
the starting node.
• With each new pass, we visit nodes that are one more edge away.
• We will either need to keep a list of the nodes we have visited or we
will need to use a variable in the node to mark it as visited to prevent
multiple visits.
• In the opposite graph, the traversal may start at node 1, nodes 2
and 8 will be visited on the first pass.
• On the second pass, we will visit nodes 3 and 7.
• (Even though nodes 2 and 8 are also at the end of paths of length 2, we will
not return to them because they were visited in the first pass.)
• On the third pass, we visit nodes 4 and 5, and on the last pass we
visit nodes 6
• and 9.
• Where the depth-first traversal depended on a stack, our breadth-
first traversal is based on a queue.
TRAVERSAL ALGORITHMS : Breadth-First
Traversal
• If we begin our traversal at node 1, we
• will visit nodes 2 and 8 on the first pass. On the second
pass, we will visit
• nodes 3 and 7. (Even though nodes 2 and 8 are also at the
end of paths of
• length 2, we will not return to them because they were
visited in the first pass.)
• On the third pass, we visit nodes 4 and 5,
• On the last pass we visit nodes 6 and 9.
• Where the depth-first traversal depended on a stack,
• our breadth-first traversal is based on a queue.
Assignment

• Write a recursive algorithm for a linear search

You might also like