Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

86
Trees and Graphs Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation www.telerik. com

Transcript of Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Page 1: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Trees and GraphsTrees, Binary Search Trees, Balanced Trees,

Graphs

Svetlin NakovTelerik

Corporationwww.telerik.com

Page 2: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Table of Contents1. Tree-like Data Structures

2. Trees and Related Terminology

3. Implementing Trees

4. Traversing Trees

5. Balanced Trees

6. Graphs

2

Page 3: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Tree-like Data Structures

Trees, Balanced Trees, Graphs, Networks

Page 4: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Tree-like Data Structures

Tree-like data structures are Branched recursive data structures

Consisting of nodes

Each node can be connected to other nodes

Examples of tree-like structures Trees: binary / other degree,

balanced, etc.

Graphs: directed / undirected, weighted, etc.

Networks

4

Page 5: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Tree-like Data Structures

5

Project Manager

Team Leader

De-signer

QA Team Leader

Developer 1

Developer 2

Tester 1Developer

3 Tester 2

Tree

Graph

2

36

1

45

7

19

21

141

12 31

4

11

5 (20)

5 (10)

15 (15)

15 (3

0) 5 (5)

20(20)

10 (40)

Network

Page 6: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Trees and Related Terminology

Node, Edge, Root, Children, Parent, Leaf , Binary Search Tree,

Balanced Tree

Page 7: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Trees Tree data structure – terminology

Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height, subtree

Height = 2

Depth 0

Depth 1

Depth 2

17

15149

6 5 8

7

Page 8: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Binary Trees Binary trees: most widespread form Each node has at most 2 children

10

17

159

6 5 8

Root node

Left subtree

Right child

Right child

Left child 8

Page 9: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Binary Search Trees Binary search trees are ordered

For each node x in the tree

All the elements of the left subtree of x are ≤ x

All the elements of the right subtree of x are > x

Binary search trees can be balanced Balanced trees have height of ~ log2(x)

Balanced trees have for each node nearly equal number of nodes in its subtrees

9

Page 10: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Binary Search Trees (2)

Example of balanced binary search tree

If the tree is balanced, add / search / delete operations take approximately log(n) steps

17

199

6 12 25

10

Page 11: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Implementing TreesRecursive Tree Data Structure

Page 12: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Recursive Tree Definition

The recursive definition for tree data structure: A single node is tree

Tree nodes can have zero or multiple children that are also trees

Tree node definition in C#

12

public class TreeNode<T>{ private T value; private List<TreeNode<T>> children; …}

The value contained in

the node

List of child nodes, which are of the

same type

Page 13: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

TreeNode<int> Structure

13

7 children

19 children 21 children 14 children

TreeNode<int>

1 children

12 children

31 children 23 children 6 children

int value

List<TreeNode<int>> children

Page 14: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Implementing TreeNode<T>

14

public TreeNode(T value){ this.value = value; this.children = new List<TreeNode<T>>();}

public T Value{ get { return this.value; } set { this.value = value; }}

public void AddChild(TreeNode<T> child){ child.hasParent = true; this.children.Add(child);}

public TreeNode<T> GetChild(int index){ return this.children[index];}

Page 15: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

The class Tree<T> keeps tree's root node

Implementing Tree<T>

15

public class Tree<T>{ private TreeNode<T> root;

public Tree(T value, params Tree<T>[] children): this(value) { foreach (Tree<T> child in children) { this.root.AddChild(child.root); } }

public TreeNode<T> Root { get { return this.root; } }}

Flexible constructor for building trees

Page 16: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Building a Tree

16

Tree<int> tree =

new Tree<int>(7,

new Tree<int>(19,

new Tree<int>(1),

new Tree<int>(12),

new Tree<int>(31)),

new Tree<int>(21),

new Tree<int>(14,

new Tree<int>(23),

new Tree<int>(6))

);

7

1419

23 6

21

311 12

Constructing tree by nested constructors:

Page 17: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Tree TraversalsDFS and BFS Traversals

Page 18: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Tree Traversal Algorithms

Traversing a tree means to visit each of its nodes exactly one in particular order Many traversal algorithms are

known

Depth-First Search (DFS)

Visit node's successors first

Usually implemented by recursion

Breadth-First Search (BFS)

Nearest nodes visited first

Implemented by a queue

18

Page 19: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Depth-First Search first visits all descendants of given node recursively, finally visits the node itself

DFS algorithm pseudo code

Depth-First Search (DFS)

DFS(node)

{

for each child c of

node

DFS(c);

print the current node;

}

7

1419

23 6

21

311 121 2 3

4 5 8

6 7

9

19

Page 20: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 1) Stack: 7 Output: (empty)

20

7

1419

23 6

21

311 12

Page 21: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 2) Stack: 7, 19 Output: (empty)

21

7

1419

23 6

21

311 12

Page 22: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 3) Stack: 7, 19, 1 Output: (empty)

22

7

1419

23 6

21

311 12

Page 23: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 4) Stack: 7, 19 Output: 1

23

7

1419

23 6

21

311 12

Page 24: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 5) Stack: 7, 19, 12 Output: 1

24

7

1419

23 6

21

311 12

Page 25: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 6) Stack: 7, 19 Output: 1, 12

25

7

1419

23 6

21

311 12

Page 26: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 7) Stack: 7, 19, 31 Output: 1, 12

26

7

1419

23 6

21

311 12

Page 27: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 8) Stack: 7, 19 Output: 1, 12, 31

27

7

1419

23 6

21

311 12

Page 28: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 9) Stack: 7 Output: 1, 12, 31, 19

28

7

1419

23 6

21

311 12

Page 29: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 10) Stack: 7, 21 Output: 1, 12, 31, 19

29

7

1419

23 6

21

311 12

Page 30: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 11) Stack: 7 Output: 1, 12, 31, 19, 21

30

7

1419

23 6

21

311 12

Page 31: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 12) Stack: 7, 14 Output: 1, 12, 31, 19, 21

31

7

1419

23 6

21

311 12

Page 32: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 13) Stack: 7, 14, 23 Output: 1, 12, 31, 19, 21

32

7

1419

23 6

21

311 12

Page 33: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 14) Stack: 7, 14 Output: 1, 12, 31, 19, 21, 23

33

7

1419

23 6

21

311 12

Page 34: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 15) Stack: 7, 14, 6 Output: 1, 12, 31, 19, 21, 23

34

7

1419

23 6

21

311 12

Page 35: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 16) Stack: 7, 14 Output: 1, 12, 31, 19, 21, 23, 6

35

7

1419

23 6

21

311 12

Page 36: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 17) Stack: 7 Output: 1, 12, 31, 19, 21, 23, 6, 14

36

7

1419

23 6

21

311 12

Page 37: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

DFS in Action (Step 18) Stack: (empty) Output: 1, 12, 31, 19, 21, 23, 6, 14, 7

37

7

1419

23 6

21

311 12

Traversal finished

Page 38: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Breadth-First Search first visits the neighbor nodes, later their neighbors, etc.

BFS algorithm pseudo code

Breadth-First Search (BFS)

BFS(node)

{

queue node

while queue not empty

v queue

print v

for each child c of v

queue c

}

7

1419

23 6

21

311 125 6 7

2 3 4

8 9

1

38

Page 39: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 1) Queue: 7 Output: 7

39

7

1419

23 6

21

311 12

Page 40: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 2) Queue: 7, 19 Output: 7

40

7

1419

23 6

21

311 12

Page 41: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 3) Queue: 7, 19, 21 Output: 7

41

7

1419

23 6

21

311 12

Page 42: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 4) Queue: 7, 19, 21, 14 Output: 7

42

7

1419

23 6

21

311 12

Page 43: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 5) Queue: 7, 19, 21, 14 Output: 7, 19

43

7

1419

23 6

21

311 12

Page 44: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 6) Queue: 7, 19, 21, 14, 1 Output: 7, 19

44

7

1419

23 6

21

311 12

Page 45: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 7) Queue: 7, 19, 21, 14, 1, 12 Output: 7, 19

45

7

1419

23 6

21

311 12

Page 46: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 8) Queue: 7, 19, 21, 14, 1, 12, 31 Output: 7, 19

46

7

1419

23 6

21

311 12

Page 47: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 9) Queue: 7, 19, 21, 14, 1, 12, 31 Output: 7, 19, 21

47

7

1419

23 6

21

311 12

Page 48: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 10) Queue: 7, 19, 21, 14, 1, 12, 31 Output: 7, 19, 21, 14

48

7

1419

23 6

21

311 12

Page 49: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 11) Queue: 7, 19, 21, 14, 1, 12, 31, 23 Output: 7, 19, 21, 14

49

7

1419

23 6

21

311 12

Page 50: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 12) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14

50

7

1419

23 6

21

311 12

Page 51: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 13) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1

51

7

1419

23 6

21

311 12

Page 52: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 14) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12

52

7

1419

23 6

21

311 12

Page 53: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 15) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31

53

7

1419

23 6

21

311 12

Page 54: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 16) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31, 23

54

7

1419

23 6

21

311 12

Page 55: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 16) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31, 23, 6

55

7

1419

23 6

21

311 12

Page 56: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

BFS in Action (Step 17) Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6 Output: 7, 19, 21, 14, 1, 12, 31, 23, 6

56

7

1419

23 6

21

311 12

The queue

is empty stop

Page 57: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Binary Trees DFS Traversals

DFS traversal of binary trees can be done in pre-order, in-order and post-order

Pre-order: left, root, right 6, 9, 12, 17, 19, 25

In-order: root, left, right 17, 9, 6, 12, 19, 25

Post-order: left, right, root 6, 12, 9, 25, 19, 17

17

199

6 12 25

57

Page 58: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Iterative DFS and BFS What will happen if in the Breadth-

First Search (BFS) algorithm a stack is used instead of queue?

An iterative Depth-First Search (DFS) – in-order

58

BFS(node)

{

queue node

while queue not empty

v queue

print v

for each child c of

v

queue c

}

DFS(node)

{

stack node

while stack not empty

v stack

print v

for each child c of

v

stack c

}

Page 59: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Trees and TraversalsLive Demo

Page 60: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Balanced Search TreesAVL Trees, B-Trees, Red-Black Trees, AA-Trees

Page 61: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Balanced Binary Search Trees

Ordered Binary Trees (Binary Search Trees) For each node x the left subtree has

values ≤ x and the right subtree has values > x

Balanced Trees For each node its subtrees contain

nearly equal number of nodes nearly the same height

Balanced Binary Search Trees Ordered binary search trees that

have height of log2(n) where n is the number of their nodes

Searching costs about log2(n) comparisons

61

Page 62: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Balanced Binary Search Tree – Example

62

33

18

15 24

3 17 20 29

54

42 60

37 43 59 85

Page 63: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Balanced Binary Search Trees

Balanced binary search trees are hard to implement

Rebalancing the tree after insert / delete is complex

Well known implementations of balanced binary search trees

AVL trees – ideally balanced, very complex

Red-black trees – roughly balanced, more simple

AA-Trees – relatively simple to implement

Find / insert / delete operations need log2(n) steps

63

Page 64: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

B-Trees B-trees are generalization of the concept of ordered binary search trees B-tree of order d has between d and

2*d keys in a node and between d+1 and 2*d+1 child nodes

The keys in each node are ordered increasingly

All keys in a child node have values between their left and right parent keys

If the b-tree is balanced, its search / insert / add operations take about log(n) steps

B-trees can be efficiently stored on the disk

64

Page 65: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

B-Tree of order 2, also known as 2-3-4-tree:

B-Tree – Example

65

17 21

711

18

20

26

31

2 4 5 6 8 912

16

22

23

25

27

29

30

32 35

Page 66: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Balanced Trees in .NET .NET Framework has several built-in implementations of balanced search trees: SortedDictionary<K,V>

Red-black tree based map of key-value pairs

OrderedSet<T> Red-black tree based set of elements

External libraries like "Wintellect Power Collections for .NET" are more flexible http://powercollections.codeplex.co

m

66

Page 67: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

GraphsDefinitions, Representation, Traversal

Algorithms

Page 68: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Data Structure Set of nodes with many-to-many

relationship between them is called graph

Each node has multiple predecessors

Each node has multiple successors

7

19

21

141

12 31

4

11

Node with multiple

predecessors

Node with

multiple success

ors

68

Page 69: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Definitions Node (vertex)

Element of graph Can have name or value Keeps a list of adjacent nodes

Edge Connection between two nodes Can be directed / undirected Can be weighted / unweighted Can have name / value

A

Node

A

Edge

B

69

Page 70: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Definitions (2) Directed graph

Edges have direction

Undirected graph Undirected

edges

7

19

21

1

12

4

322 2

3

G

J

F

D

A

E C H

70

Page 71: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Definitions (3) Weighted graph

Weight (cost) is associated with each edge

G

J

F

D

A

E C H

Q

K

N

104

14

6 16

9

8

7

5

22

3

71

Page 72: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Definitions (4) Path (in undirected graph)

Sequence of nodes n1, n2, … nk

Edge exists between each pair of nodes ni, ni+1

Examples:

A, B, C is a path

H, K, C is not a path

G

CB

A

H N

K

72

Page 73: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Definitions (5) Path (in directed graph)

Sequence of nodes n1, n2, … nk

Directed edge exists between each pair of nodes ni, ni+1

Examples:

A, B, C is a path

A, G, K is not a path

G

CB

A

H N

K

73

Page 74: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Definitions (6) Cycle

Path that ends back at the starting node

Example:

A, B, C, G, A

Simple path No cycles in path

Acyclic graph Graph with no cycles

Acyclic undirected graphs are trees

G

CB

A

H N

K

74

Page 75: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Unconnected graph with two

connected component

s

Graph Definitions (8) Two nodes are reachable if

Path exists between them Connected graph

Every node is reachable from any other node

G

JF

D

A

Connected graph

G

J

F

D

A

E C H

75

Page 76: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graphs and Their Applications

Graphs have many real-world applications Modeling a computer network like

Internet

Routes are simple paths in the network

Modeling a city map

Streets are edges, crossings are vertices

Social networks

People are nodes and their connections are edges

State machines

States are nodes, transitions are edges

76

Page 77: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Representing Graphs

Adjacency list

Each node holds a list of its neighbors

Adjacency matrix

Each cell keeps whether and how two nodes are connected

Set of edges

0 1 0 1

0 0 1 0

1 0 0 0

0 1 0 0

1

2

3

4

1 2 3 4

{1,2} {1,4} {2,3} {3,1} {4,2}

1 {2, 4}2 {3}3 {1}4 {2}

2

41

3

77

Page 78: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Representing Graphs in C#

public class Graph{ int[][] childNodes; public Graph(int[][] nodes) { this.childNodes = nodes; }}

Graph g = new Graph(new int[][] { new int[] {3, 6}, // successors of vertice 0 new int[] {2, 3, 4, 5, 6}, // successors of vertice 1 new int[] {1, 4, 5}, // successors of vertice 2 new int[] {0, 1, 5}, // successors of vertice 3 new int[] {1, 2, 6}, // successors of vertice 4 new int[] {1, 2, 3}, // successors of vertice 5 new int[] {0, 1, 4} // successors of vertice 6});

06

41

52

3

78

Page 79: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graph Traversal Algorithms

Depth-First Search (DFS) and Breadth-First Search (BFS) can traverse graphs Each vertex should be is visited at

most once

79

BFS(node){ queue node visited[node] = true while queue not empty v queue print v for each child c of v if not visited[c] queue c visited[c] = true}

DFS(node){ stack node visited[node] = true while stack not empty v stack print v for each child c of v if not visited[c] stack c visited[c] = true}

Page 80: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Recursive DFS Graph Traversal

80

void TraverseDFSRecursive(node){ if (not visited[node]) { visited[node] = true print node foreach child node c of node { TraverseDFSRecursive(c); } }}

vois Main(){ TraverseDFS(firstNode);}

Page 81: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Graphs and TraversalsLive Demo

Page 82: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Summary Trees are recursive data structure –

node with set of children which are also nodes

Binary Search Trees are ordered binary trees

Balanced trees have weight of log(n) Graphs are sets of nodes with many-

to-many relationship between them

Can be directed/undirected, weighted / unweighted, connected / not connected, etc.

Tree / graph traversals can be done by Depth-First Search (DFS) and Breadth-First Search (BFS)

82

Page 83: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Trees and Graphs

Questions??

?

????

??

?

http://academy.telerik.com

Page 84: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Exercises

1. Write a program to traverse the directory C:\WINDOWS and all its subdirectories recursively and to display all files matching the mask *.exe. Use the class System.IO.Directory.

2. Define classes File { string name, int size } and Folder { string name, File[] files, Folder[] childFolders } and using them build a tree keeping all files and folders on the hard drive starting from C:\WINDOWS. Implement a method that calculates the sum of the file sizes in given subtree of the tree and test it accordingly. Use recursive DFS traversal.

84

Page 85: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Exercises (2)

3. Implement the recursive Depth-First-Search (DFS) traversal algorithm. Test it with the sample graph from the demonstrations.

4. Implement the queue-based Breath-First-Search (BFS) traversal algorithm. Test it with the sample graph from the demonstrations.

5. Write a program for finding all cycles in given undirected graph using recursive DFS.

6. Write a program for finding all connected components of given undirected graph. Use a sequence of DFS traversals.

85

Page 86: Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation .

Exercises (3)

7. Write a program for finding the shortest path between two vertices in a weighted directed graph. Hint: Use the Dijkstra's algorithm.

8. We are given a set of N tasks that should be executed in a sequence. Some of the tasks depend on other tasks. We are given a list of tasks { ti, tj} where tj depends on the result of ti and should be executed after it. Write a program that arranges the tasks in a sequence so that each task depending on another task is executed after it. If such arrangement is impossible indicate this fact.

Example: {1, 2}, {2, 5}, {2, 4}, {3, 1} 3, 1, 2, 5, 4

86