Sie sind auf Seite 1von 25

Advanced Data Structures

Chapter IV:
Data Structures

FAP:UC-BCF

Contents
Graphs
- Terminology
- Graph Representation
- Graph Traversal
- Spanning Trees
- Kruskals Algorithm

FAP:UC-BCF

Graphs
A graph G, consists of two sets V and E.
V is a finite non-empty set of vertices. V(G) = {Vi, VjVn}
E is the set of pairs of vertices. E(G) = {(Vi,Vj),(Vk,Vl)}
These pair of vertices are called edges.
Example of Undirected Graphs G1 and G2:
1

4
FAP:UC-BCF

Kinds of Graphs
Undirected Graph

The pair of vertices representing any edge in unordered.


Uses a pair of parentheses to denote an edge in an
undirected graph
(V1, V2) = (V2, V1)

Directed Graph

The pair of vertices representing any edge is ordered.


Uses angled brackets to denote edges in a directed graph
Also known as digraph
<V1,V2> != <V2, V1>
<V1,V2> where V1 = tail, V2 = head.

FAP:UC-BCF

Kinds of Graphs
Undirected Graph

The maximum number of edges in any n vertex is n(n-1)/2


Complete undirected graph

Directed Graph

The maximum number of edges in any n vertex is n(n-1).


Complete directed graph

FAP:UC-BCF

Graphs
Adjacent and Incidents
1

Consider the edge <1, 2> in graph G3:


Vertex 1 is adjacent to vertex 2, and vertex 2 is

adjacent from vertex 1.


The edge <1, 2> is incident to both vertices 1 & 2

FAP:UC-BCF

Vertex 2 is adjacent to vertex 3, vertex 3 is adjacent from


vertex 2.

Graphs
Adjacent and Incidents
1

Vertex 1 is adjacent to vertices 4, 2


4

Vertex 2, and 4 are adjacent from vertex 1


Edges that are incident to vertex 1 are:

<1, 2>, <1, 4>, <3, 1>


3
5

FAP:UC-BCF

Graphs
Adjacent and Incidents

FAP:UC-BCF

Vertices adjacent to vertex 2 are:


1, 4, 5
Edges incident to vertex 2 are:
(1, 2), (2,4), (2, 5)

Subgraphs
A subgraph of G is a graph G such that V(G)<=V(G)
And E(G)<=E(G).
G1

Some Subgraphs of G1

FAP:UC-BCF

Graphs
Path

A walk from one vertex to another along directed edges

Simple Path

A path wherein all vertices except the first and the last are
distinct

Cycle

A simple path where the first and the last vertices are the
same.

FAP:UC-BCF

10

Graphs
Example:
1

1
1

4
2

G1

Simple path of G1:


1, 2, 3, 4
4, 3, 1, 2
Cycle of G1:
1, 3, 2, 4, 1
3, 2, 4, 3
FAP:UC-BCF

G2

Simple path of G2:


1, 2, 4
1, 3, 6

3
5

G3

Simple path of G3:


1, 2, 5
5, 2
Cycle of G3:
1, 4, 3, 1
11

Graphs

Degree of a vertex (Directed Graph)

In-degree

The number of edges for which the vertex is the head/2nd component.

Out-degree

The number of edges for which the vertex is the tail/1st component.

Degree of a vertex v = in-degree of v + out-degree of v

Degree of a vertex (Undirected Graph)


Number of edges incident to that vertex.

FAP:UC-BCF

12

Graphs
Example:

1
1
4

G4

Degree of vertex 2 = 3
Degree of vertex 3 = 2

FAP:UC-BCF

3
5

G3

Degree of vertex 1 = 3
where in-degree of vertex 1 = 1
plus out-degree of vertex 1 = 2

13

Graphs

Connected
An undirected/directed graph is said to be connected if for
every pair of distinct vertices Vi, Vj in V(G), there is a path from
Vi to Vj.

Strongly Connected (Directed graph)


A directed graph is said to be strongly connected if for every
pair of vertices Vi, Vj in V(G), there is a directed path from Vi to
Vj and from Vj to Vi

FAP:UC-BCF

14

Graph Representation
Adjacency Matrix
Adjacency matrix of an N vertex graph G is a two-dimensional array
(NxN), say A, with the property such that A[i,j] = 1 IFF the edge
(Vi, Vj) ((<Vi, Vj>) for directed graph) is in E(G). Otherwise, A[i,j]=0
1

G3

FAP:UC-BCF

Note:
Row sum of vertex1 = out-degree of vertex1
Column sum of vertex1 = in-degree of vertex1
15

Graphs
1

G4

1
2
3
4
5
6

0
1
1
0
0
0

1
0
0
1
1
0

1
0
0
0
0
1

0
1
0
0
0
0

0
1
0
0
0
0

0
0
1
0
0
0

Note:
Row sum of vertex1 = degree of vertex1

FAP:UC-BCF

16

Graph Representation
Adjacency List
N rows of the adjacency matrix are represented as N linked lists.
The nodes in each list represented the vertices adjacent from vertexl.
Each node has at least 2 fields: vertex and link.
1

G3

FAP:UC-BCF

17

Graph Traversals

DFS (Depth First Search)


Algorithm:
The starting vertex v is visited
An unvisited vertex w adjacent to v is selected and a DFS
from w is initiated
When a vertex u is reached such that all its adjacent vertices
have been visited, back up to the last vertex visited which
has an unvisited vertex w adjacent to it and initiate DFS from
w.
The search terminates when no visited vertex can be reached
from any of the unvisited ones.

FAP:UC-BCF

18

Graph Traversals

BFS (Breadth First Search)


Algorithm:
Starting at vertex v and marking it as visited. In BFS, all
unvisited vertices adjacent to v are visited next.
Then, the unvisited adjacent to these vertices are visited and
so on.
Note:
In case of multiple adjacent vertices, visit the first vertex wit
the lowest data value.

FAP:UC-BCF

19

Graph Traversals
Example:
1

DFS @1: 1, 2, 4, 8, 5, 6, 3, 7

*lowest data value

BFS @1: 1, 2, 3, 4, 5, 6, 7, 8

*lowest data value

FAP:UC-BCF

20

Graph Traversals
7

Exercise:

8
4

12
1

10

13

3
5

11

14
6

DFS @7: 7, 4, 1, 2, 5, 3, 6, 12, 13, 14, 9, 10, 11, 8

*lowest data value

DFS @8: 8, 4, 7, 3, 14, 13, 12, 6, 5, 2, 11, 10, 9, 1

*highest data value

BFS @7:

7, 4, 1, 2, 3, 8, 5, 6, 9, 10, 11, 12, 13, 14

*lowest data value

BFS @8:

8, 4, 7, 3, 2, 1, 14, 13, 12, 6, 5, 11, 10, 9

*highest data value

FAP:UC-BCF

21

Spanning Trees

A connected non-cyclic graph


A minimal connected subgraph (by minimal, it means
one with the fewest number of edges.)
Any tree consisting solely of edges in the graph
including all vertices in the graph.
DF spanning tree
BF spanning tree

FAP:UC-BCF

22

Spanning Trees
1

DF Spanning Tree

FAP:UC-BCF

BF Spanning Tree

23

Minimum Cost Spanning Trees

Finding a spanning tree with the minimum cost.


The cost of the spanning tree is the sum of all the costs
of the edges in that tree
One approach = Kruskals algorithm

FAP:UC-BCF

24

Kruskals ALgorithm
The minimum cost spanning tree is built, edge by edge. An edge
is included in the tree if it does not form a cycle with edges
already in the tree.
Algorithm:

T:={}
While T contains less than n-1 edges and E is not empty do
Begin
Choose an edge(v,w) from E of lowest cost;
Delete (v,w) from E.
If(v,w) does not create a cycle in T then
Add (v,w) to T
else
Discard (v,w)
End;
If T contains fewer than n-1 edges then writeln(No spanning tree)
Note: E is a set of Edges in G. T will contain edges for the final minimum cost spanning tree.
FAP:UC-BCF

25

Das könnte Ihnen auch gefallen