17 trees and graphs

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

Upload: maznabili

Post on 26-Jun-2015

124 views

Category:

Technology


1 download

DESCRIPTION

Trees and graphs

TRANSCRIPT

Page 1: 17 Trees and graphs

Trees and GraphsTrees and GraphsTrees, Binary Search Trees, Balanced Trees, Binary Search Trees, Balanced

Trees, GraphsTrees, Graphs

Svetlin NakovSvetlin NakovTelerik Telerik

CorporationCorporationwww.telerik.com

Page 2: 17 Trees and graphs

Table of ContentsTable of Contents

1.1. Tree-like Data StructuresTree-like Data Structures

2.2. Trees and Related TerminologyTrees and Related Terminology

3.3. Implementing TreesImplementing Trees

4.4. Traversing TreesTraversing Trees

5.5. Balanced TreesBalanced Trees

6.6. GraphsGraphs

2

Page 3: 17 Trees and graphs

Tree-like Data Tree-like Data StructuresStructures

Trees, Balanced Trees, Graphs, NetworksTrees, Balanced Trees, Graphs, Networks

Page 4: 17 Trees and graphs

Tree-like Data Tree-like Data StructuresStructures

Tree-like data structures areTree-like data structures are Branched recursive data structuresBranched recursive data structures

Consisting of nodesConsisting of nodes

Each node can be connected to other Each node can be connected to other nodesnodes

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

etc.etc.

Graphs: directed / undirected, weighted, Graphs: directed / undirected, weighted, etc.etc.

NetworksNetworks 4

Page 5: 17 Trees and graphs

Tree-like Data Tree-like Data StructuresStructures

5

Project Manager

Team Leader

De-signer

QA Team Leader

Developer 1

Developer 2

Tester 1

Developer 3

Tester 2

TreTreee

GraGraphph

2

36

1

45

7

19

21

141

12 31

4

11

5 (20)

5 (10)

15 (15)

15 (30

) 5 (5)

20(20)

10 (40)

NetwoNetworkrk

Page 6: 17 Trees and graphs

Trees and Related Trees and Related TerminologyTerminology

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

Balanced TreeBalanced Tree

Page 7: 17 Trees and graphs

TreesTrees TreeTree data structure – terminology data structure – terminology

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

Height = 2Height = 2

Depth 0Depth 0

Depth 1Depth 1

Depth 2Depth 2

1717

1515141499

66 55 88

7

Page 8: 17 Trees and graphs

Binary TreesBinary Trees Binary treesBinary trees: most widespread form: most widespread form

Each node has at most 2 childrenEach node has at most 2 children

1010

1717

151599

66 55 88

Root Root nodenode

Left Left subtreesubtree

Right Right childchild

Right Right childchild

Left Left childchild 8

Page 9: 17 Trees and graphs

Binary Search TreesBinary Search Trees Binary search trees Binary search trees are are orderedordered

For each node For each node xx in the tree in the tree

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

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

Binary search trees can be Binary search trees can be balancedbalanced Balanced trees Balanced trees have height of have height of ~ log~ log22((xx))

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

Page 10: 17 Trees and graphs

Binary Search Trees (2)Binary Search Trees (2)

Example of balanced binary search Example of balanced binary search treetree

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

1717

191999

66 1212 2525

10

Page 11: 17 Trees and graphs

Implementing TreesImplementing TreesRecursive Tree Data StructureRecursive Tree Data Structure

Page 12: 17 Trees and graphs

Recursive Tree Recursive Tree DefinitionDefinition

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

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

Tree node definition in C#Tree node definition in C#

12

public class TreeNode<T>public class TreeNode<T>

{{

private T value;private T value;

private List<TreeNode<T>> children;private List<TreeNode<T>> children;

… …

}}

The value The value contained in contained in

the nodethe node

List of child List of child nodes, which are nodes, which are of the same typeof the same type

Page 13: 17 Trees and graphs

TreeNode<int>TreeNode<int> Structure Structure

13

77 childrenchildren

1919 childrenchildren 2121 childrenchildren 1414 childrenchildren

TreeNode<inTreeNode<int>t>

11 childrenchildren

1212 childrenchildren

3131 childrenchildren 2323 childrenchildren 66 childrenchildren

int int valuevalue

List<TreeNode<int>> List<TreeNode<int>> childrenchildren

Page 14: 17 Trees and graphs

Implementing Implementing TreeNodeTreeNode<T><T>

14

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

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

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

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

Page 15: 17 Trees and graphs

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

Implementing Implementing Tree<T>Tree<T>

15

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

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

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

Flexible Flexible constructor for constructor for building treesbuilding trees

Page 16: 17 Trees and graphs

Building a TreeBuilding a Tree

16

Tree<int> tree =Tree<int> tree =

new Tree<int>(7,new Tree<int>(7,

new Tree<int>(19,new Tree<int>(19,

new Tree<int>(1),new Tree<int>(1),

new Tree<int>(12),new Tree<int>(12),

new Tree<int>(31)),new Tree<int>(31)),

new Tree<int>(21),new Tree<int>(21),

new Tree<int>(14,new Tree<int>(14,

new Tree<int>(23),new Tree<int>(23),

new Tree<int>(6))new Tree<int>(6))

););

77

14141919

2323 66

2121

313111 1212

Constructing tree by nested Constructing tree by nested constructors:constructors:

Page 17: 17 Trees and graphs

Tree TraversalsTree TraversalsDFS and BFS TraversalsDFS and BFS Traversals

Page 18: 17 Trees and graphs

Tree Traversal Tree Traversal AlgorithmsAlgorithms

Traversing a treeTraversing a tree means to visit each means to visit each of its nodes exactly one in particular of its nodes exactly one in particular orderorder Many traversal algorithms are knownMany traversal algorithms are known

Depth-First Search (DFS)Depth-First Search (DFS)

Visit node's successors firstVisit node's successors first

Usually implemented by recursionUsually implemented by recursion

Breadth-First Search (BFS)Breadth-First Search (BFS)

Nearest nodes visited firstNearest nodes visited first

Implemented by a queueImplemented by a queue18

Page 19: 17 Trees and graphs

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

DFS algorithm pseudo codeDFS algorithm pseudo code

Depth-First Search Depth-First Search (DFS)(DFS)

DFS(node)DFS(node)

{{

for each child for each child cc of of

nodenode

DFS(DFS(cc););

print the current node;print the current node;

}}

77

14141919

2323 66

2121

313111 12121 2 3

4 5 8

6 7

9

19

Page 20: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 11)) Stack: Stack: 77 Output: Output: (empty)(empty)

20

77

14141919

2323 66

2121

313111 1212

Page 21: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 22)) Stack: Stack: 77, , 1919 Output: Output: (empty)(empty)

21

77

14141919

2323 66

2121

313111 1212

Page 22: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 33)) Stack: Stack: 77, , 1919, , 11 Output: Output: (empty)(empty)

22

77

14141919

2323 66

2121

313111 1212

Page 23: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 44)) Stack: Stack: 77, , 1919 Output: Output: 11

23

77

14141919

2323 66

2121

313111 1212

Page 24: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 55)) Stack: Stack: 77, , 1919, , 1212 Output: Output: 11

24

77

14141919

2323 66

2121

313111 1212

Page 25: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 66)) Stack: Stack: 77, , 1919 Output: Output: 11, , 1212

25

77

14141919

2323 66

2121

313111 1212

Page 26: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 77)) Stack: Stack: 77, , 1919, , 3131 Output: Output: 11, , 1212

26

77

14141919

2323 66

2121

313111 1212

Page 27: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 88)) Stack: Stack: 77, , 1919 Output: Output: 11, , 1212, , 3131

27

77

14141919

2323 66

2121

313111 1212

Page 28: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 99)) Stack: Stack: 77 Output: Output: 11, , 1212, , 3131, , 1919

28

77

14141919

2323 66

2121

313111 1212

Page 29: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1010)) Stack: Stack: 77, , 2121 Output: Output: 11, , 1212, , 3131, , 1919

29

77

14141919

2323 66

2121

313111 1212

Page 30: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1111)) Stack: Stack: 77 Output: Output: 11, , 1212, , 3131, , 1919, , 2121

30

77

14141919

2323 66

2121

313111 1212

Page 31: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1212)) Stack: Stack: 77, , 1414 Output: Output: 11, , 1212, , 3131, , 1919, , 2121

31

77

14141919

2323 66

2121

313111 1212

Page 32: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1313)) Stack: Stack: 77, , 1414, , 2323 Output: Output: 11, , 1212, , 3131, , 1919, , 2121

32

77

14141919

2323 66

2121

313111 1212

Page 33: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1414)) Stack: Stack: 77, , 1414 Output: Output: 11, , 1212, , 3131, , 1919, , 2121, , 2323

33

77

14141919

2323 66

2121

313111 1212

Page 34: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1515)) Stack: Stack: 77, , 1414, , 66 Output: Output: 11, , 1212, , 3131, , 1919, , 2121, , 2323

34

77

14141919

2323 66

2121

313111 1212

Page 35: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1616)) Stack: Stack: 77, , 1414 Output: Output: 11, , 1212, , 3131, , 1919, , 2121, , 2323, , 66

35

77

14141919

2323 66

2121

313111 1212

Page 36: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1717)) Stack: Stack: 77 Output: Output: 11, , 1212, , 3131, , 1919, , 2121, , 2323, , 66, , 1414

36

77

14141919

2323 66

2121

313111 1212

Page 37: 17 Trees and graphs

DFS in Action (Step DFS in Action (Step 1818)) Stack: Stack: (empty)(empty) Output: Output: 11, , 1212, , 3131, , 1919, , 2121, , 2323, , 66, , 1414, , 77

37

77

14141919

2323 66

2121

313111 1212

Traversal Traversal finishedfinished

Page 38: 17 Trees and graphs

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

BFS algorithm pseudo codeBFS algorithm pseudo code

Breadth-First Search Breadth-First Search (BFS)(BFS)

BFS(node)BFS(node)

{{

queue queue node node

while queue not emptywhile queue not empty

vv queue queue

print vprint v

for each child for each child cc of of vv

queue queue cc

}}

77

14141919

2323 66

2121

313111 12125 6 7

2 3 4

8 9

1

38

Page 39: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 11)) Queue: Queue: 77 Output: Output: 77

39

77

14141919

2323 66

2121

313111 1212

Page 40: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 22)) Queue: Queue: 77, , 1919 Output: Output: 77

40

77

14141919

2323 66

2121

313111 1212

Page 41: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 33)) Queue: Queue: 77, , 1919, , 2121 Output: Output: 77

41

77

14141919

2323 66

2121

313111 1212

Page 42: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 44)) Queue: Queue: 77, , 1919, , 2121, , 1414 Output: Output: 77

42

77

14141919

2323 66

2121

313111 1212

Page 43: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 55)) Queue: Queue: 77, , 1919, , 2121, , 1414 Output: Output: 77, , 1919

43

77

14141919

2323 66

2121

313111 1212

Page 44: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 66)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11 Output: Output: 77, , 1919

44

77

14141919

2323 66

2121

313111 1212

Page 45: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 77)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212 Output: Output: 77, , 1919

45

77

14141919

2323 66

2121

313111 1212

Page 46: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 88)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131 Output: Output: 77, , 1919

46

77

14141919

2323 66

2121

313111 1212

Page 47: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 99)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131 Output: Output: 77, , 1919, , 2121

47

77

14141919

2323 66

2121

313111 1212

Page 48: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1010)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131 Output: Output: 77, , 1919, , 2121, , 1414

48

77

14141919

2323 66

2121

313111 1212

Page 49: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1111)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323 Output: Output: 77, , 1919, , 2121, , 1414

49

77

14141919

2323 66

2121

313111 1212

Page 50: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1212)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414

50

77

14141919

2323 66

2121

313111 1212

Page 51: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1313)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414, , 11

51

77

14141919

2323 66

2121

313111 1212

Page 52: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1414)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414, , 11, , 1122

52

77

14141919

2323 66

2121

313111 1212

Page 53: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1515)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414, , 11, , 1122, , 3131

53

77

14141919

2323 66

2121

313111 1212

Page 54: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1616)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414, , 11, , 1122, , 3131, , 2323

54

77

14141919

2323 66

2121

313111 1212

Page 55: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1616)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414, , 11, , 1122, , 3131, , 2323, , 66

55

77

14141919

2323 66

2121

313111 1212

Page 56: 17 Trees and graphs

BFS in Action (Step BFS in Action (Step 1717)) Queue: Queue: 77, , 1919, , 2121, , 1414, , 11, , 1212, , 3131, , 2323, , 66 Output: Output: 77, , 1919, , 2121, , 1414, , 11, , 1122, , 3131, , 2323, , 66

56

77

14141919

2323 66

2121

313111 1212

The The queue queue

is is empty empty stop stop

Page 57: 17 Trees and graphs

Binary TreesBinary Trees DFS DFS TraversalsTraversals

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

Pre-order: left, root, right Pre-order: left, root, right 66, , 99, , 1212, , 1717, , 1919, , 2525 In-order: root, left, right In-order: root, left, right 1717, , 99, , 66, , 1212, , 1919, , 2525 Post-order: left, right, root Post-order: left, right, root 66, , 1212, , 99, , 2525, , 1919, , 1717

1717

191999

66 1212 2525

57

Page 58: 17 Trees and graphs

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

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

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

58

BFS(node)BFS(node)

{{

queue queue node node

while queue not emptywhile queue not empty

vv queue queue

print vprint v

for each child for each child cc of of

vv

queue queue cc

}}

DFS(node)DFS(node)

{{

stack stack node node

while stack not emptywhile stack not empty

vv stack stack

print vprint v

for each child for each child cc of of

vv

stack stack cc

}}

Page 59: 17 Trees and graphs

Trees and TraversalsTrees and TraversalsLive DemoLive Demo

Page 60: 17 Trees and graphs

Balanced Search Balanced Search TreesTreesAVL Trees, B-Trees, Red-Black Trees, AA-AVL Trees, B-Trees, Red-Black Trees, AA-TreesTrees

Page 61: 17 Trees and graphs

Balanced Binary Search Balanced Binary Search TreesTrees

Ordered Binary Trees (Ordered Binary Trees (Binary Search Binary Search TreesTrees)) For each node For each node xx the left subtree has values the left subtree has values

≤ x≤ x and the right subtree has values and the right subtree has values > x> x Balanced TreesBalanced Trees

For each node its subtrees contain nearly For each node its subtrees contain nearly equal number of nodes equal number of nodes nearly the same nearly the same heightheight

Balanced Binary Search TreesBalanced Binary Search Trees Ordered binary search trees that have Ordered binary search trees that have

height of height of loglog22(n)(n) where where nn is the number of is the number of their nodestheir nodes

Searching costs about Searching costs about loglog22(n)(n) comparisons comparisons 61

Page 62: 17 Trees and graphs

Balanced Binary Search Balanced Binary Search Tree – ExampleTree – Example

62

3333

1818

1515 2424

33 1717 2020 2929

5454

4242 6060

3737 4343 5959 8585

Page 63: 17 Trees and graphs

Balanced Binary Search Balanced Binary Search TreesTrees

Balanced binary search trees are hard to Balanced binary search trees are hard to implementimplement

Rebalancing the tree after insert / delete is Rebalancing the tree after insert / delete is complexcomplex

Well known implementations of balanced Well known implementations of balanced binary search treesbinary search trees

AVL trees – ideally balanced, very complexAVL trees – ideally balanced, very complex

Red-black trees – roughly balanced, more Red-black trees – roughly balanced, more simplesimple

AA-Trees – relatively simple to implementAA-Trees – relatively simple to implement Find / insert / delete operations need Find / insert / delete operations need loglog22(n)(n)

stepssteps 63

Page 64: 17 Trees and graphs

B-TreesB-Trees B-treesB-trees are generalization of the concept are generalization of the concept

of ordered binary search treesof ordered binary search trees B-tree of order B-tree of order dd has between has between dd and and 2*d2*d

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

The keys in each node are ordered The keys in each node are ordered increasinglyincreasingly

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

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

B-trees can be efficiently stored on the B-trees can be efficiently stored on the diskdisk 64

Page 65: 17 Trees and graphs

B-Tree of order B-Tree of order 22, , also known as also known as 22--33--44-tree:-tree:

B-Tree – ExampleB-Tree – Example

65

1717 2121

771111

1188

2200

2266

3311

22 44 55 66 88 991122

1166

2222

2233

2255

2277

2299

3300

3232 3535

Page 66: 17 Trees and graphs

Balanced Trees in .NETBalanced Trees in .NET .NET Framework has several built-in .NET Framework has several built-in

implementations of balanced search implementations of balanced search trees:trees: SortedDictionary<K,V>SortedDictionary<K,V>

Red-black tree based map of key-value Red-black tree based map of key-value pairspairs

OrderedSet<T>OrderedSet<T>

Red-black tree based set of elementsRed-black tree based set of elements

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

66

Page 67: 17 Trees and graphs

GraphsGraphsDefinitions, Representation, Traversal Definitions, Representation, Traversal

AlgorithmsAlgorithms

Page 68: 17 Trees and graphs

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

relationship between them is called relationship between them is called graphgraph

Each node has Each node has multiplemultiple predecessors predecessors

Each node has Each node has multiplemultiple successors successors

77

1919

2121

141411

1212 3131

44

1111

Node with Node with multiple multiple

predecessorspredecessors

Node Node with with

multiple multiple successsuccess

orsors

68

Page 69: 17 Trees and graphs

Graph DefinitionsGraph Definitions Node Node ((vertexvertex))

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

EdgeEdge Connection between two nodesConnection between two nodes Can be directed / undirectedCan be directed / undirected Can be weighted / unweightedCan be weighted / unweighted Can have name / valueCan have name / value

AA

NodeNode

AA

EdgeEdge

BB

69

Page 70: 17 Trees and graphs

Graph Definitions (2)Graph Definitions (2) Directed graphDirected graph

Edges have directionEdges have direction

Undirected Undirected graphgraph Undirected Undirected

edgesedges

77

1919

2121

11

1212

44

332222

22

33

GG

JJ

FF

DD

AA

EE CC HH

70

Page 71: 17 Trees and graphs

Graph Definitions (3)Graph Definitions (3) Weighted graphWeighted graph

Weight (cost) is associated with Weight (cost) is associated with each edgeeach edge

GG

JJ

FF

DD

AA

EE CC HH

QQ

KK

NN

104

14

6 16

9

8

7

5

22

3

71

Page 72: 17 Trees and graphs

Graph Definitions (4)Graph Definitions (4) Path Path (in undirected graph)(in undirected graph)

Sequence of nodes nSequence of nodes n11, n, n22, … , … nnkk

Edge exists between each pair of Edge exists between each pair of nodes nodes nnii,, nni+1i+1

Examples:Examples:

A, B, C A, B, C is a pathis a path

H, K, C H, K, C is not a pathis not a path GG

CCBB

AA

HH NN

KK

72

Page 73: 17 Trees and graphs

Graph Definitions (5)Graph Definitions (5) Path Path (in directed graph)(in directed graph)

Sequence of nodes nSequence of nodes n11, n, n22, … , … nnkk

Directed edge exists between each Directed edge exists between each pair of nodes pair of nodes nnii,, nni+1i+1

Examples:Examples:

A, B, C A, B, C is a pathis a path

A, G, K A, G, K is not a pathis not a path GG

CCBB

AA

HH NN

KK

73

Page 74: 17 Trees and graphs

Graph Definitions (6)Graph Definitions (6) CycleCycle

Path that ends back at the starting Path that ends back at the starting nodenode

Example:Example:

A, B, C, G, AA, B, C, G, A Simple pathSimple path

No cycles in pathNo cycles in path Acyclic graphAcyclic graph

Graph with no cyclesGraph with no cycles

Acyclic undirected graphs are treesAcyclic undirected graphs are trees

GG

CCBB

AA

HH NN

KK

74

Page 75: 17 Trees and graphs

UnconnecteUnconnected graph d graph with two with two

connected connected componentcomponent

ss

Graph Definitions (8)Graph Definitions (8) Two nodes are Two nodes are reachablereachable if if

Path exists between themPath exists between them Connected graphConnected graph

Every node is reachable from any Every node is reachable from any other nodeother node

GG

JJFF

DD

AA

Connected Connected graphgraph

GG

JJFF

DD

AA

EE CC HH

75

Page 76: 17 Trees and graphs

Graphs and Their Graphs and Their ApplicationsApplications

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

Routes are simple paths in the networkRoutes are simple paths in the network

Modeling a city mapModeling a city map

Streets are edges, crossings are verticesStreets are edges, crossings are vertices

Social networksSocial networks

People are nodes and their connections are People are nodes and their connections are edgesedges

State machinesState machines

States are nodes, transitions are edgesStates are nodes, transitions are edges

76

Page 77: 17 Trees and graphs

Representing GraphsRepresenting Graphs Adjacency listAdjacency list

Each node holds a Each node holds a list of its list of its neighborsneighbors

Adjacency matrixAdjacency matrix

Each cell keeps Each cell keeps whether and how whether and how two nodes are two nodes are connectedconnected

Set of edgesSet of edges

00 11 00 11

00 00 11 00

11 00 00 00

00 11 00 00

1

2

3

4

1 2 3 4

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

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

22

4411

33

77

Page 78: 17 Trees and graphs

Representing Graphs in Representing Graphs in C#C#

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

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

0066

4411

55

22

33

78

Page 79: 17 Trees and graphs

Graph Traversal Graph Traversal AlgorithmsAlgorithms

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

most oncemost once

79

BFS(BFS(nodenode)){{ queue queue nodenode visited[visited[nodenode] = true] = true while queue not emptywhile queue not empty vv queue queue print print vv for each child for each child cc of of vv if not visited[if not visited[cc]] queue queue cc visited[visited[cc] = ] = truetrue}}

DFS(DFS(nodenode)){{ stack stack nodenode visited[visited[nodenode] = true] = true while stack not emptywhile stack not empty vv stack stack print print vv for each child for each child cc of of vv if not visited[if not visited[cc]] stack stack cc visited[visited[cc] = ] = truetrue}}

Page 80: 17 Trees and graphs

Recursive DFS Graph Recursive DFS Graph TraversalTraversal

80

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

vois Main()vois Main(){{ TraverseDFS(firstNode);TraverseDFS(firstNode);}}

Page 81: 17 Trees and graphs

Graphs and TraversalsGraphs and TraversalsLive DemoLive Demo

Page 82: 17 Trees and graphs

SummarySummary Trees are recursive data structure – node Trees are recursive data structure – node

with set of children which are also nodeswith set of children which are also nodes Binary Search Trees are ordered binary Binary Search Trees are ordered binary

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

many relationship between themmany relationship between them

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

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

82

Page 83: 17 Trees and graphs

Trees and GraphsTrees and Graphs

Questions?Questions?

http://academy.telerik.com

Page 84: 17 Trees and graphs

ExercisesExercises

1.1. Write a program to traverse the directory Write a program to traverse the directory C:\C:\

WINDOWSWINDOWS and all its subdirectories recursively and all its subdirectories recursively and to display all files matching the mask and to display all files matching the mask *.exe*.exe. Use the class . Use the class System.IO.DirectorySystem.IO.Directory..

2.2. Define classes Define classes FileFile {{ stringstring name,name, intint sizesize }} and and FolderFolder {{ stringstring name,name, File[] files File[] files,, Folder[]Folder[]

childFolderschildFolders }} and using them build a tree and using them build a tree keeping all files and folders on the hard drive keeping all files and folders on the hard drive starting from starting from C:\WINDOWSC:\WINDOWS. Implement a . Implement a method that calculates the sum of the file method that calculates the sum of the file sizes in given subtree of the tree and test it sizes in given subtree of the tree and test it accordingly. Use recursive DFS traversal.accordingly. Use recursive DFS traversal.

84

Page 85: 17 Trees and graphs

Exercises (2)Exercises (2)

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

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

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

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

85

Page 86: 17 Trees and graphs

Exercises (3)Exercises (3)

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

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

Example: {Example: {11, , 22}, {}, {22, , 55}, {}, {22, , 44}, {}, {33, , 11} } 33, , 11, , 22, , 55, , 44

86