0% found this document useful (0 votes)
205 views23 pages

Directed Vs Undirected Graph

The document discusses directed and undirected graphs. It defines graphs formally as a set of vertices and edges. Directed graphs have edges (v, w) that may or may not be the same as (w, v), while undirected graphs always have symmetric edges. Examples of graphs in real life include road maps, computer networks, and molecular structures. The document also covers graph traversal algorithms like breadth-first search (BFS) and depth-first search (DFS).

Uploaded by

Marilyn Low
Copyright
© Attribution Non-Commercial (BY-NC)
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)
205 views23 pages

Directed Vs Undirected Graph

The document discusses directed and undirected graphs. It defines graphs formally as a set of vertices and edges. Directed graphs have edges (v, w) that may or may not be the same as (w, v), while undirected graphs always have symmetric edges. Examples of graphs in real life include road maps, computer networks, and molecular structures. The document also covers graph traversal algorithms like breadth-first search (BFS) and depth-first search (DFS).

Uploaded by

Marilyn Low
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 23

1

Directed and Undirected Graphs


A graph is a mathematical structure consisting of a set of vertices and a set of edges connecting the vertices. Formally: G = (V, E ), where V is a set and E V V . G = (V, E ) undirected if for all v, w V : (v, w) E (w, v ) E. Otherwise directed.

A directed graph
G = (V, E ) with vertex set V = 0, 1, 2, 3, 4, 5, 6 and edge set E = (0, 2), (0, 4), (0, 5), (1, 0), (2, 1), (2, 5), (3, 1), (3, 6), (4, 0), (4, 5), (6, 3), (6, 5) .

An undirected graph
1 2 0 10 11 4 9 12 8 6 7 5

Examples of graphs in real life


Road Maps. Edges represent streets and vertices represent crossings.

Examples (contd)

Airline route maps. Vertices represent airports, and there is an edge from vertex A to vertex B if there is a direct ight from the airport represented by A to the airport represented by B .

Examples (contd)
Electrical Circuits. Vertices represent diodes, transistors, capacitors, switches, etc., and edges represent wires connecting them.

Examples (contd)

Computer Networks. Vertices represent computers and edges represent network connections (cables) between them. The World Wide Web. Vertices represent webpages, and edges represent hyperlinks.

Examples (contd)

Flowcharts. Vertices represent boxes and edges represent arrows. Conict graphs in scheduling problems. Example: Assignment of time slots to exams. No overlapping time slots for exams taken by the same students. Modeled by graph whose vertices represent the exams, with an edge between two vertices if there is a student who has to take both exams.

10

Examples (contd)
Molecules. Vertices are atoms, edges are bonds between them.

H H C H

H C H O H

11

Examples (contd)
Binary Relations in Mathematics. For example, the is a proper divisor relation on the integers.
8

10

12

The adjacency matrix data structure

Let G = (V, E ) be a graph with n vertices. Vertices of G numbered 0, . . . , n 1. The adjacency matrix of G is the n n matrix A = (aij )0i,j n1 with aij = 1 0 if there is an edge from vertex i to vertex j otherwise.

13

Example

0 1 0 0 1 0 0

0 0 1 1 0 0 0

1 0 0 0 0 0 0

0 0 0 0 0 0 1

1 0 0 0 0 0 0

1 0 1 0 1 0 1

0 0 0 1 0 0 0

14

The adjacency list data structure


Array with one entry for each vertex v , which is a list of all vertices adjacent to v . Example

0 1 2

2 0 1 1 0

5 6 5

3 4 5

15

Adjacency matrices vs adjacency lists


n = number of vertices, m = number of edges adjacency matrix Space Time to check if w adjacent to v Time to visit all w adjacent to v Time to visit all edges (n2) (1) (n) (n2) adjacency list (n + m) (out-degree(v )) (out-degree(v )) (n + m)

16

Sparse and dense graphs

G = (V, E ) graph with n vertices and m edges Observation: m n2

G dense if m close to n2 G sparse if m much smaller than n2

17

Graph traversals
A traversal is a strategy for visiting all vertices of a graph. BFS = breadth-rst search DFS = depth-rst search General strategy: 1. Let v be an arbitrary vertex 2. Visit all vertices reachable from v 3. If there are vertices that have not been visited, let v be such a vertex and go back to (2)

18

BFS
Visit all vertices reachable from v in the following order: v all neighbours of v all neighbours of neighbours of v that have not been visited yet all neighbours of neighbours of neighbours of v that have not been visited yet etc.

19

BFS (contd)
Algorithm bfs(G)
1. 2. 3. 4. 5.

Initialise Boolean array visited by setting all entries to false Initialise Queue Q for all v V do if visited [v ] = false then bfsFromVertex(G, v )

20

BFS (contd)
Algorithm bfsFromVertex(G, v )
1. 2. 3. 4. 5. 6. 7. 8.

visited [v ] = true Q.enqueue(v ) while not Q.isEmpty() do v Q.dequeue() for all w adjacent to v do if visited [w] = false then visited [w] = true Q.enqueue(w)

21

DFS
Visit all vertices reachable from v in the following order: v some neighbor w of v that has not been visited yet some neighbor x of w that has not been visited yet etc., until the current vertex has no neighbour that has not been visited yet Backtrack to the rst vertex that has a yet unvisited neighbour v . Continue with v , a neighbour, a neighbour of the neighbour, etc., backtrack, etc.

22

DFS (contd)
Algorithm dfs(G)
1. 2. 3. 4. 5.

Initialise Boolean array visited by setting all entries to false Initialise Stack S for all v V do if visited [v ] = false then dfsFromVertex(G, v )

23

DFS (contd)
Algorithm dfsFromVertex(G, v )
1. 2. 3. 4. 5. 6. 7.

S.push(v ) while not S.isEmpty() do v S.pop() if visited [v ] = false then visited [v ] = true for all w adjacent to v do S.push(w)

You might also like