Trees and GraphsTrees and GraphsTrees, Binary Search Trees, Balanced Trees, Binary Search Trees, Balanced
Trees, GraphsTrees, Graphs
Svetlin NakovSvetlin NakovTelerik Telerik
CorporationCorporationwww.telerik.com
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
Tree-like Data Tree-like Data StructuresStructures
Trees, Balanced Trees, Graphs, NetworksTrees, Balanced Trees, Graphs, Networks
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
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
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
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
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
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
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
Implementing TreesImplementing TreesRecursive Tree Data StructureRecursive Tree Data Structure
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
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
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];}}
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
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:
Tree TraversalsTree TraversalsDFS and BFS TraversalsDFS and BFS Traversals
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
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
DFS in Action (Step DFS in Action (Step 11)) Stack: Stack: 77 Output: Output: (empty)(empty)
20
77
14141919
2323 66
2121
313111 1212
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
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
DFS in Action (Step DFS in Action (Step 44)) Stack: Stack: 77, , 1919 Output: Output: 11
23
77
14141919
2323 66
2121
313111 1212
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
BFS in Action (Step BFS in Action (Step 11)) Queue: Queue: 77 Output: Output: 77
39
77
14141919
2323 66
2121
313111 1212
BFS in Action (Step BFS in Action (Step 22)) Queue: Queue: 77, , 1919 Output: Output: 77
40
77
14141919
2323 66
2121
313111 1212
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
}}
Trees and TraversalsTrees and TraversalsLive DemoLive Demo
Balanced Search Balanced Search TreesTreesAVL Trees, B-Trees, Red-Black Trees, AA-AVL Trees, B-Trees, Red-Black Trees, AA-TreesTrees
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
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
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
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
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
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
GraphsGraphsDefinitions, Representation, Traversal Definitions, Representation, Traversal
AlgorithmsAlgorithms
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
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
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
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
KK
NN
104
14
6 16
9
8
7
5
22
3
71
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
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
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
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
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
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
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
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}}
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);}}
Graphs and TraversalsGraphs and TraversalsLive DemoLive Demo
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
Trees and GraphsTrees and Graphs
Questions?Questions?
http://academy.telerik.com
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
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
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