Trees-and-Graphs

advertisement
Trees and Graphs
Trees, Binary Search Trees, Balanced Trees, Graphs
Graph Fundamentals
Telerik Algo Academy
http://academy.telerik.com
Table of Contents
1.
Tree-like Data Structures
2.
Trees and Related Terminology
3.
Traversing Trees
4.
Graphs
 Graph Definitions
 Representing Graphs
 Graph Traversal Algorithms
 Connected Components
2
Tree-like Data Structures
Trees, Balanced Trees, Graphs, Networks
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
Tree-like Data Structures
Network
Tree
2
1
Project
Manager
6
3
Team
Leader
Developer
1
Designer
Developer
3
QA Team
Leader
4
5
Tester
2
Tester 1
Developer
2
7
1
14
4
21
Graph
11
12
19
31
5
Trees and Related
Terminology
Node, Edge, Root, Children, Parent, Leaf ,
Binary Search Tree, Balanced Tree
Trees
 Tree data structure – terminology
 Node, edge, root, child, children, siblings, parent,
ancestor, descendant, predecessor, successor,
internal node, leaf, depth, height, subtree
Depth 0
17
9
Height = 3
6
15
14
5
8
Depth 1
Depth 2
7
Binary Trees
 Binary
trees: most widespread form
 Each node has at most 2 children
Root node
Right child
17
Left subtree
9
6
Right child
15
5
8
10
Left child
8
Tree Traversals
DFS and BFS Traversals
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
16
Depth-First Search (DFS)
 Depth-First Search
first visits all descendants
of given node recursively, finally visits the
node itself
 DFS algorithm
pseudo code
9
7
DFS(node)
{
for each child c of node
DFS(c);
print the current node;
}
5
4
19
2
1
1
21
3
12
8
6
31
14
7
23
6
17
DFS in Action (Step 1)
 Stack: 7
 Output: (empty)
7
19
1
12
21
31
14
23
6
18
DFS in Action (Step 2)
 Stack: 7, 19
 Output: (empty)
7
19
1
12
21
31
14
23
6
19
DFS in Action (Step 3)
 Stack: 7, 19, 1
 Output: (empty)
7
19
1
12
21
31
14
23
6
20
DFS in Action (Step 4)
 Stack: 7, 19
 Output: 1
7
19
1
12
21
31
14
23
6
21
DFS in Action (Step 5)
 Stack: 7, 19, 12
 Output: 1
7
19
1
12
21
31
14
23
6
22
DFS in Action (Step 6)
 Stack: 7, 19
 Output: 1, 12
7
19
1
12
21
31
14
23
6
23
DFS in Action (Step 7)
 Stack: 7, 19, 31
 Output: 1, 12
7
19
1
12
21
31
14
23
6
24
DFS in Action (Step 8)
 Stack: 7, 19
 Output: 1, 12, 31
7
19
1
12
21
31
14
23
6
25
DFS in Action (Step 9)
 Stack: 7
 Output: 1, 12, 31, 19
7
19
1
12
21
31
14
23
6
26
DFS in Action (Step 10)
 Stack: 7, 21
 Output: 1, 12, 31, 19
7
19
1
12
21
31
14
23
6
27
DFS in Action (Step 11)
 Stack: 7
 Output: 1, 12, 31, 19, 21
7
19
1
12
21
31
14
23
6
28
DFS in Action (Step 12)
 Stack: 7, 14
 Output: 1, 12, 31, 19, 21
7
19
1
12
21
31
14
23
6
29
DFS in Action (Step 13)
 Stack: 7, 14, 23
 Output: 1, 12, 31, 19, 21
7
19
1
12
21
31
14
23
6
30
DFS in Action (Step 14)
 Stack: 7, 14
 Output: 1, 12, 31, 19, 21, 23
7
19
1
12
21
31
14
23
6
31
DFS in Action (Step 15)
 Stack: 7, 14, 6
 Output: 1, 12, 31, 19, 21, 23
7
19
1
12
21
31
14
23
6
32
DFS in Action (Step 16)
 Stack: 7, 14
 Output: 1, 12, 31, 19, 21, 23, 6
7
19
1
12
21
31
14
23
6
33
DFS in Action (Step 17)
 Stack: 7
 Output: 1, 12, 31, 19, 21, 23, 6, 14
7
19
1
12
21
31
14
23
6
34
DFS in Action (Step 18)
 Stack: (empty)
 Output: 1, 12, 31, 19, 21, 23, 6, 14, 7
Traversal finished
7
19
1
12
21
31
14
23
6
35
Breadth-First Search (BFS)
 Breadth-First Search first visits
the neighbor
nodes, later their neighbors, etc.
 BFS algorithm pseudo code
BFS(node)
{
queue  node
while queue not empty
v  queue
print v
for each child c of v
queue  c
}
1
7
3
2
19
6
5
1
21
7
12
4
8
31
14
9
23
6
36
BFS in Action (Step 1)
 Queue: 7
 Output: 7
7
19
1
12
21
31
14
23
6
37
BFS in Action (Step 2)
 Queue: 7, 19
 Output: 7
7
19
1
12
21
31
14
23
6
38
BFS in Action (Step 3)
 Queue: 7, 19, 21
 Output: 7
7
19
1
12
21
31
14
23
6
39
BFS in Action (Step 4)
 Queue: 7, 19, 21, 14
 Output: 7
7
19
1
12
21
31
14
23
6
40
BFS in Action (Step 5)
 Queue: 7, 19, 21, 14
 Output: 7, 19
7
19
1
12
21
31
14
23
6
41
BFS in Action (Step 6)
 Queue: 7, 19, 21, 14, 1
 Output: 7, 19
7
19
1
12
21
31
14
23
6
42
BFS in Action (Step 7)
 Queue: 7, 19, 21, 14, 1, 12
 Output: 7, 19
7
19
1
12
21
31
14
23
6
43
BFS in Action (Step 8)
 Queue: 7, 19, 21, 14, 1, 12, 31
 Output: 7, 19
7
19
1
12
21
31
14
23
6
44
BFS in Action (Step 9)
 Queue: 7, 19, 21, 14, 1, 12, 31
 Output: 7, 19, 21
7
19
1
12
21
31
14
23
6
45
BFS in Action (Step 10)
 Queue: 7, 19, 21, 14, 1, 12, 31
 Output: 7, 19, 21, 14
7
19
1
12
21
31
14
23
6
46
BFS in Action (Step 11)
 Queue: 7, 19, 21, 14, 1, 12, 31, 23
 Output: 7, 19, 21, 14
7
19
1
12
21
31
14
23
6
47
BFS in Action (Step 12)
 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
 Output: 7, 19, 21, 14
7
19
1
12
21
31
14
23
6
48
BFS in Action (Step 13)
 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
 Output: 7, 19, 21, 14, 1
7
19
1
12
21
31
14
23
6
49
BFS in Action (Step 14)
 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
 Output: 7, 19, 21, 14, 1, 12
7
19
1
12
21
31
14
23
6
50
BFS in Action (Step 15)
 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
 Output: 7, 19, 21, 14, 1, 12, 31
7
19
1
12
21
31
14
23
6
51
BFS in Action (Step 16)
 Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
 Output: 7, 19, 21, 14, 1, 12, 31, 23
7
19
1
12
21
31
14
23
6
52
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
7
19
1
12
21
31
14
23
6
53
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
7
19
1
12
21
31
The
queue is
empty 
stop
14
23
6
54
Binary Trees DFS Traversals
 DFS traversal
of binary trees can be done in preorder, in-order and post-order
17
9
6
19
12
25
 Pre-order: root, left, right  17, 9, 6, 12, 19, 25
 In-order: left, root, right  6, 9, 12, 17, 19, 25
 Post-order: left, right, root  6, 12, 9, 25, 19, 17
55
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
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
}
56
Graphs
Definitions, Representation, Traversal Algorithms
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
Node with multiple
predecessors
Node with
multiple
successors
7
1
14
4
21
11
12
19
31
58
Graph Definitions
 Node (vertex)
Node
 Element of graph
 Can have name or value
A
 Keeps a list of adjacent nodes
 Edge
Edge
 Connection between two nodes
 Can be directed / undirected
A
B
 Can be weighted / unweighted
 Can have name / value
59
Graph Definitions (2)
 Directed graph
 Undirected graph
 Edges have direction
22
2
3
A
F
J
7
1
4
D
G
21
12
3
 Undirected edges
19
E
C
H
60
Graph Definitions (3)
 Weighted graph
 Weight (cost) is associated with each edge
10
A
6
Q
D
E
14
F
16
4
5
J
8
7
9
C
G
N
22
3
H
K
61
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
B
 H, K, C is not a path
A
C
K
G
H
N
62
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
B
A
C
K
G
H
N
63
Graph Definitions (6)
 Cycle
 Path that ends back at the starting node
 Example:
 A, B, C, G, A
B
 Simple path
 No cycles in path
A
C
K
G
 Acyclic graph
 Graph with no cycles
H
N
 Acyclic undirected graphs are trees
64
Graph Definitions (7)
 Two nodes are
reachable if
Unconnected
graph with two
connected
components
 Path exists between them
 Connected graph
 Every node is reachable from any other node
Connected graph
A
F
J
F
A
J
D
G
D
E
G
C
H
65
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
66
Representing Graphs

Adjacency list
 Each node holds a
list of its neighbors

Adjacency matrix
 Each cell keeps
whether and how two
nodes are connected

1
2
3
4
Set of edges




{2, 4}
{3}
{1}
{2}
1
2
3
4
1
0
1
0
1
2
0
0
1
0
3
1
0
0
0
4
0
1
0
0
3
2
1
4
{1,2} {1,4} {2,3} {3,1} {4,2}
67
Representing Graphs in C#
public class Graph
{
int[][] childNodes;
public Graph(int[][] nodes)
{
this.childNodes = nodes;
}
}
Graph
new
new
new
new
new
new
new
});
6
0
3
1
4
5
2
g = new Graph(new int[][] {
int[] {3, 6}, // successors of vertice 0
int[] {2, 3, 4, 5, 6}, // successors of vertice 1
int[] {1, 4, 5}, // successors of vertice 2
int[] {0, 1, 5}, // successors of vertice 3
int[] {1, 2, 6}, // successors of vertice 4
int[] {1, 2, 3}, // successors of vertice 5
int[] {0, 1, 4} // successors of vertice 6
68
Graph Traversal Algorithms

Depth-First Search (DFS) and Breadth-First
Search (BFS) can traverse graphs
 Each vertex should be is visited at most once
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
}
69
Recursive DFS Graph Traversal
void TraverseDFSRecursive(node)
{
if (not visited[node])
{
visited[node] = true
print node
foreach child node c of node
{
TraverseDFSRecursive(c);
}
}
}
vois Main()
{
TraverseDFS(firstNode);
}
70
Graphs and Traversals
Live Demo
Connectivity
Connecting the chain
Connectivity
 Connected component of undirected graph
 A sub-graph in which any two nodes are
connected to each other by paths
73
Connectivity (2)
 A simple way to find number of connected
components
 A loop through all nodes and start a DFS or BFS
traversing from any unvisited node
 Each time you start a new traversing
 You find a new connected component!
74
Connectivity (3)
 Algorithm:
foreach node from graph G
{
if node is unvisited
{
DFS(node);
countOfComponents++;
}
}
*Note: Do not forget to mark each node in the DFS as visited!
A
F
C
H
D
G
75
Connectivity (4)
 Connected graph
 A graph with only one connected
component
 In every connected graph a path
exists between any two nodes
 Checking whether a graph is
connected
 If DFS / BFS passes through all
vertices  graph is connected!
76
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)
77
Trees and Graphs
Questions?
http://algoacademy.telerik.com
Download