0% found this document useful (0 votes)
23 views38 pages

Dayschool 5

Uploaded by

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

Dayschool 5

Uploaded by

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

Graphs

• Definition of Graphs and Related


Concepts
• Representation of Graphs
• The Graph Class
• Graph Traversal
• Graph Applications
1
Definition of Graphs
• A graph is a finite set of nodes with edges
between nodes
• Formally, a graph G is a structure (V,E)
consisting of
– a finite set V called the set of nodes, and
– a set E that is a subset of VxV. That is, E is a set
of pairs of the form (x,y) where x and y are nodes
in V

2
Examples of Graphs
• V={0,1,2,3,4}
• E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
1
0

2
4
3

3
A “Real-life” Example of a Graph
• V=set of 6 people: John, Mary, Joe, Helen,
Tom, and Paul, of ages 12, 15, 12, 15, 13,
and 13, respectively.
• E ={(x,y) | if x is younger than y}
Mary Helen

John Joe

Tom Paul
4
Graph Representation
• For graphs to be computationally useful,
they have to be conveniently represented in
programs
• There are two computer representations of
graphs:
– Adjacency matrix representation
– Adjacency lists representation

5
Adjacency Matrix Representation

• In this representation, each graph of n nodes


is represented by an n x n matrix A, that is,
a two-dimensional array A
• The nodes are labeled 0,1,2,…,n
• A[i][j] = 1 if (i,j) is an edge
• A[i][j] = 0 if (i,j) is not an edge

6
Example of Adjacency Matrix
0 1 2 3 4
1
0 1 0 1 0 0
0 1 2 3 4

A= 0 0 1 0 0
0 0 1 0 0 2
1 0 0 0 0 4
0 0 0 4 0 3

7
Another Example of Adj. Matrix
• Re-label the nodes with numerical labels
0 1 2 3 4 5
0 0 0 0 0 0 Mary 0 Helen 1
0 1 2 3 4 5

0 0 0 0 0 0
A= Joe 3
1 1 0 0 1 1
John 2
1 1 0 0 1 1
1 1 0 0 0 0
1 1 0 0 0 0 Tom 4 Paul 5

8
Pros and Cons of Adjacency
Matrices
• Pros:
– Simple to implement
– Easy and fast to tell if a pair (i,j) is an edge:
simply check if A[i][j] is 1 or 0
• Cons:
– No matter how few edges the graph has, the
matrix takes O(n2) in memory

9
Adjacency Lists Representation
• A graph of n nodes is represented by a one-
dimensional array L of linked lists, where
– L[i] is the linked list containing all the nodes
adjacent from node i.
– The nodes in the list L[i] are in no particular
order

10
Example of Linked Representation
L[0]: empty Mary 0 Helen 1

L[1]: empty Joe


John 2 3
L[2]: 0, 1, 4, 5
L[3]: 0, 1, 4, 5 Tom 4 Paul 5

L[4]: 0, 1
L[5]: 0, 1

11
Pros and Cons of Adjacency Lists
• Pros:
– Saves on space (memory): the representation
takes as many memory words as there are nodes
and edge.
• Cons:
– It can take up to O(n) time to determine if a pair
of nodes (i,j) is an edge: one would have to
search the linked list L[i], which takes time
proportional to the length of L[i].
12
Directed vs. Undirected Graphs
• If the directions of the edges matter, then
we show the edge directions, and the graph
is called a directed graph (or a digraph)
• The previous two examples are digraphs
• If the relationships represented by the edges
are symmetric (such as (x,y) is edge if and
only if x is a sibling of y), then we don’t
show the directions of the edges, and the
graph is called an undirected graph.
13
Examples of Undirected Graphs
• V=set of 6 people: John, Mary, Joe, Helen,
Tom, and Paul, where the first 4 are siblings,
and the last two are siblings
• E ={(x,y) | x and y are siblings}
Mary Helen if (x,y) is an edge:
we say that x is
John Joe adjacent to y, &
y adjacent to x.
We also say that
x and y are
Tom Paul
neighbors
14
Representations of Undirected
Graphs
• The same two representations for directed
graphs can be used for undirected graphs
• Adjacency matrix A:
– A[i][j]=1 if (i,j) is an edge; 0 otherwise
• Adjacency Lists:
– L[i] is the linked list containing all the
neighbors of i

15
Example of Representations
Mary 0 Helen 1
Linked Lists:
John 2 Joe 3
L[0]: 1, 2, 3
L[1]: 0, 2, 3 Tom 4 Paul 5
Adjacency
L[2]: 0, 1, 3 Matrix:
0 1 1 1 0 0
L[3]: 0, 1, 2 1 0 1 1 0 0
A=
L[4]: 5 1 1 0 1 0 0
1 1 1 0 0 0
L[5]: 4 0 0 0 0 0 1
0 0 0 0 1 0
16
Definition of Some Graph Related
Concepts
• Let G be a directed graph
– The indegree of a node x in G is the number of
edges coming to x
– The outdegree of x is the number of edges
leaving x.
• Let G be an undirected graph
– The degree of a node x is the number of edges
that have x as one of their end nodes
– The neighbors of x are the nodes adjacent to x
17
Paths
• A path in a graph G is a sequence
of nodes x1, x2, …,xk, such that
there is an edge from each node
the next one in the sequence
• For example, in the first example
graph, the sequence 3, 0, 1, 2 is a
path, but the sequence 0, 3, 4 is
not a path because (0,3) is not an
edge
• In the “sibling-of” graph, the
sequence John, Mary, Joe,
Helen is a path, but the sequence
Helen, Tom, Paul is not a path

18
Graph Connectivity
• An undirected graph is said to be connected
if there is a path between every pair of
nodes. Otherwise, the graph is disconnected
• Informally, an undirected graph is
connected if it hangs in one piece

Disconnected Connected
19
Connected Components
• If an undirected graph is not connected, then each
“piece” is called a connected component.
– A piece in itself is connected, but if you bring any other
node to it from the graph, it is no longer connected
• If the graph is connected, then the whole graph is
one single connected component

20
Graph Traversal Techniques
• The previous connectivity problem, as well
as many other graph problems, can be
solved using graph traversal techniques
• There are two standard graph traversal
techniques:
– Depth-First Search (DFS)
– Breadth-First Search (BFS)

21
Graph Traversal (Contd.)
• In both DFS and BFS, the nodes of the
undirected graph are visited in a systematic
manner so that every node is visited exactly
one.
• Both BFS and DFS give rise to a tree:
– When a node x is visited, it is labeled as visited,
and it is added to the tree
– If the traversal go to node x from node y, y is
viewed as the parent of x, and x a child of y
22
Depth-First Search
• DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.
23
Illustration of DFS
0 1
0
2
1 4
4 9
10 5 7
2
9 11 8
5 6
7 11
6 Graph G
8 10
DFS Tree

24
25
Implementation of DFS
• Observations:
– the last node visited is the first node from
which to proceed.
– Also, the backtracking proceeds on the basis of
"last visited, first to backtrack too".
– This suggests that a stack is the proper data
structure to remember the current node and how
to backtrack.

26
DFS (Pseudo Code)
DFS(G,v) ( v is the vertex where the search starts )
Stack S := {}; ( start with an empty stack )
for each vertex u, set visited[u] := false;
push S, v;
while (S is not empty) do
u := pop S;
if (not visited[u]) then
visited[u] := true;
for each unvisited neighbour w of u
push S, w;
end if
end while
END DFS()
27
Depth-First Search (DFS)

28
Breadth-First Search
• BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
2. From each node z in the current level, in the order in
which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes from
this level form a new level that becomes the next
current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.

29
Illustration of BFS
0 1
0
2 4 2
1 4
9
5 9 10
10 5 7
11 8
6 7 8 11
6
BFS Tree Graph G

30
Implementation of BFS
• Observations:
– the first node visited in each level is the first
node from which to proceed to visit new nodes.

• This suggests that a queue is the proper data


structure to remember the order of the steps.

31
BFS (Pseudo Code)
BFS (G, s) //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are
marked.

mark s as visited.
while ( Q is not empty)
//Removing that vertex from queue,whose neighbour will be
visited now
v = Q.dequeue( )
//processing all the neighbours of v
for all neighbours w of v in Graph G
if w is not visited
Q.enqueue( w ) //Stores w in Q to further visit its
neighbour
mark w as visited.
32
Breadth-First Search (BFS)

33
Shortest Path Problems
• How can we find the shortest route between two
points on a road map?
• Model the problem as a graph problem:
– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)

34
Shortest Path Problem
• Input: t 6
x
3 9
– Directed graph G = (V, E) 3
4
2 1
– Weight function w : E → R s 0 2 7
5 3
• Weight of path p = v0, v1, . . . , vk 5 6 11
k
w( p )   w( vi 1 , vi ) y z
i 1

• Shortest-path weight from u to v:


p
δ(u, v) = min w(p) : u v if there exists a path from u to v

∞ otherwise
• Note: there might be multiple shortest paths from u to v 35
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.
Works on both directed and undirected graphs. However, all
edges must have nonnegative weights.

Approach: Greedy

Input: Weighted graph G={E,V} and source vertex v∈V,


such that all edge weights are nonnegative

Output: Lengths of shortest paths (or the shortest paths


themselves) from a given source vertex v∈V to all other
vertices
36
The Bellman-Ford Algorithm
• There is another algorithm, which is due to
Bellman and Ford, that can find shortest paths in
graphs that have negative-weight edges
• However, we must assume in this case that the
graph is directed (otherwise we could traverse
along a negative-weight edge back and forth for as
long as we needed ending up with a path as light
as we wanted)

37
The Bellman-Ford Algorithm
• The Bellman-Ford algorithm shares the
notion of edge relaxation from Dijkstra’s
algorithm, however
• It does not use it in conjunction with the
greedy method, but rather
• Performs a relaxation of every edge in a
digraph exactly (n-1) times

38

You might also like