Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Teamwork and Personal Skills Course Introduction Software University SoftUni Team Technical Trainers.
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Data Structures Curriculum, Trainers, Evaluation, Exams SoftUni Team Technical Trainers Software University
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Sets, Dictionaries SoftUni Team Technical Trainers Software University
SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees,
Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Graphs and Graph Algorithms
Basic Tree Data Structures
Fast String Manipulation
Fundamentals, Terminology, Traversal, Algorithms
Csc 2720 Instructor: Zhuojun Duan
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University

2 1.Graph Definitions and Terminology 2.Representing Graphs 3.Graph Traversal Algorithms  Depth-First-Search (DFS)  Breadth-First-Search (BFS) 4.Connected Components 5.Topological Sorting  Source Removal and DFS Algorithms Table of Contents

Graphs Definitions and Terminology

4  Graph, denoted as G(V, E)  Set of nodes V with many-to-many relationship between them (edges E )  Each node (vertex) has multiple predecessors and multiple successors  Edges connect two nodes (vertices) Graph Data Structure Node with multiple predecessors Node with multiple successors Self-relationship (loop) Node Edge

5  Node (vertex)  Element of a graph  Can have name / value  Keeps a list of adjacent nodes  Edge  Connection between two nodes  Can be directed / undirected  Can be weighted / unweighted  Can have name / value Graph Definitions A Node A Edge B

6  Directed graph  Edges have direction Graph Definitions (2)  Undirected graph  Undirected edges G J F D A E C H

7  Weighted graph  Weight (cost) is associated with each edge Graph Definitions (3) G J F D A E C H Q K N

8  Path (in undirected graph)  Sequence of nodes n 1, n 2, … n k  Edge exists between each pair of nodes n i, n i+1  Examples:  A, B, C is a path  A, B, G, N, K is a path  H, K, C is not a path  H, G, G, B, N is not a path Graph Definitions (4) G C B A HN K

9  Path (in directed graph)  Sequence of nodes n 1, n 2, … n k  Directed edge exists between each pair of nodes n i, n i+1  Examples:  A, B, C is a path  N, G, A, B, C is a path  A, G, K is not a path  H, G, K, N is not a path Graph Definitions (5) G C B A HN K

10  Cycle  Path that ends back at the starting node  Example of cycle: A, B, C, G, A  Simple path  No cycles in path  Acyclic graph  Graph with no cycles  Acyclic undirected graphs are trees Graph Definitions (6) G C B A H N K

11  Two nodes are reachable if а path exists between them  Connected graph  Every two nodes are reachable from each other Graph Definitions (7) G J F D A Connected graph G J F D A E C H Unconnected graph holding two connected components

12  Graphs have many real-world applications  Modeling a computer network like the 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 Graphs and Their Applications

Representing Graphs Classic and OOP Ways

14  Adjacency list  Each node holds a list of its neighbors  Adjacency matrix  Each cell keeps whether and how two nodes are connected  List of edges Representing Graphs {1,2} {1,4} {2,3} {3,1} {4,2} 1  {2, 4} 2  {3} 3  {1} 4  {2}

15 Graph Representation: Adjacency List var g = new List [] { new List {3, 6}, new List {3, 6}, new List {2, 3, 4, 5, 6}, new List {2, 3, 4, 5, 6}, new List {1, 4, 5}, new List {1, 4, 5}, new List {0, 1, 5}, new List {0, 1, 5}, new List {1, 2, 6}, new List {1, 2, 6}, new List {1, 2, 3}, new List {1, 2, 3}, new List {0, 1, 4} new List {0, 1, 4}}; // Add an edge { 3  6 } g[3].Add(6); // List the children of node #1 var childNodes = g[1];

16 Graph Representation: Adjacency Matrix var graph = new[,] { // { 0, 0, 0, 1, 0, 0, 1 }, // node 0 { 0, 0, 0, 1, 0, 0, 1 }, // node 0 { 0, 0, 1, 1, 1, 1, 1 }, // node 1 { 0, 0, 1, 1, 1, 1, 1 }, // node 1 { 0, 1, 0, 0, 1, 1, 0 }, // node 2 { 0, 1, 0, 0, 1, 1, 0 }, // node 2 { 1, 1, 0, 0, 0, 1, 0 }, // node 3 { 1, 1, 0, 0, 0, 1, 0 }, // node 3 { 0, 1, 1, 0, 0, 0, 1 }, // node 4 { 0, 1, 1, 0, 0, 0, 1 }, // node 4 { 0, 1, 1, 1, 0, 0, 0 }, // node 5 { 0, 1, 1, 1, 0, 0, 0 }, // node 5 { 1, 1, 0, 0, 1, 0, 0 }, // node 6 { 1, 1, 0, 0, 1, 0, 0 }, // node 6}; // Add an edge { 3  6 } g[3, 6] = 1; // List the children of node #1 var childNodes = g[1];

17 Graph Representation: List of Edges class Edge { public int Parent {get; set; } public int Parent {get; set; } public int Child {get; set; } } public int Child {get; set; } } var graph = new List () { new Edge() { Parent = 0, Child = 3 }, new Edge() { Parent = 0, Child = 3 }, new Edge() { Parent = 0, Child = 6 }, new Edge() { Parent = 0, Child = 6 }, …} // Add an edge { 3  6 } graph.Add(new Edge() { Parent = 3, Child = 6 }); // List the children of node #1 var childNodes = graph.Where(e => e.Parent == 1);

18  A common technique to speed up working with graphs  Numbering the nodes and accessing them by index (not by name) Numbering Graph Nodes Ruse Plovdiv Bourgas Sofia Stara Zagora Pleven Varna Graph of numbered nodes: [0…6] Graph of named nodes

19  Suppose we have graphs of n nodes  We can assign a number for each node in the range [0…n-1] Numbering Graph Nodes – How? var g = new List [n] new List [n] var g = new Dictionary< string, List > string, List > #Node 0Ruse 1Sofia 2Pleven 3Varna 4Bourgas 5Stara Zagora 6Plovdiv

20 Graph of Named Nodes – Example var graph = new Dictionary >() { { "Sofia", new List () { { "Sofia", new List () { "Plovdiv", "Varna", "Bourgas", "Pleven", "Stara Zagora" } }, "Plovdiv", "Varna", "Bourgas", "Pleven", "Stara Zagora" } }, { "Plovdiv", new List () { { "Plovdiv", new List () { "Bourgas", "Ruse" } }, "Bourgas", "Ruse" } }, { "Varna", new List () { { "Varna", new List () { "Ruse", "Stara Zagora" } }, "Ruse", "Stara Zagora" } }, { "Bourgas", new List () { { "Bourgas", new List () { "Plovdiv", "Pleven" } }, "Plovdiv", "Pleven" } }, { "Ruse", new List () { { "Ruse", new List () { "Varna", "Plovdiv" } }, "Varna", "Plovdiv" } }, { "Pleven", new List () { { "Pleven", new List () { "Bourgas", "Stara Zagora" } }, "Bourgas", "Stara Zagora" } }, { "Stara Zagora", new List () { { "Stara Zagora", new List () { "Varna", "Pleven" } }, "Varna", "Pleven" } },};

21 Graph of Numbered Nodes – Example public class Graph { List [] childNodes; List [] childNodes; string[] nodeNames; string[] nodeNames;} Graph g = new Graph(new List [] { new List {3, 6}, // children of node 0 (Ruse) new List {3, 6}, // children of node 0 (Ruse) new List {2, 3, 4, 5, 6}, // successors of node 1 (Sofia) new List {2, 3, 4, 5, 6}, // successors of node 1 (Sofia) new List {1, 4, 5}, // successors of node 2 (Pleven) new List {1, 4, 5}, // successors of node 2 (Pleven) new List {0, 1, 5}, // successors of node 3 (Varna) new List {0, 1, 5}, // successors of node 3 (Varna) new List {1, 2, 6}, // successors of node 4 (Bourgas) new List {1, 2, 6}, // successors of node 4 (Bourgas) new List {1, 2, 3}, // successors of node 5 (Stara Zagora) new List {1, 2, 3}, // successors of node 5 (Stara Zagora) new List {0, 1, 4} // successors of node 6 (Plovdiv) new List {0, 1, 4} // successors of node 6 (Plovdiv)}, new string[] {"Ruse", "Sofia", "Pleven", "Varna", "Bourgas", … });

22  Using OOP:  Class Node  Class Edge ( Connection )  Class Graph  Optional classes  Algorithm classes  Using external graph and algorithms library:  QuickGraph – OOP-Based-Graph Representation

Representing Graphs Live Demo

Graphs Traversals Depth-First Search (DFS) and Breadth-First Search (BFS)

25  Traversing a graph means to visit each of its nodes exactly once  The order of visiting nodes may vary on the traversal algorithm  Depth-First Search (DFS)  Visit node's successors first  Usually implemented by recursion  Breadth-First Search (BFS)  Nearest nodes visited first  Implemented with a queue Graph Traversal Algorithms

26  Depth-First Search (DFS) first visits all descendants of given node recursively, finally visits the node itself Depth-First Search (DFS) visited[0 … n-1] = false; for (v = 0 … n-1) DFS(v) DFS (node) { if not visited[node] { if not visited[node] { visited[node] = true; visited[node] = true; for each child c of node for each child c of node DFS(c); DFS(c); print node; print node; }}

27  Stack: 1  Output: (empty) DFS in Action (Step 1) Start DFS from the initial node

28  Stack: 1, 19  Output: (empty) DFS in Action (Step 2) Enter recursively into the first child

29  Stack: 1, 19, 7  Output: (empty) DFS in Action (Step 3) Enter recursively into the first child 7

30  Stack: 1, 19, 7, 1  Output: (empty) DFS in Action (Step 4) Enter recursively into the first child 1

31  Stack: 1, 19, 7  Output: (empty) DFS in Action (Step 5) Node 1 already visited  return back

32  Stack: 1, 19  Output: 7 DFS in Action (Step 6) Return back from recursion and print the last visited node Print node 7

33  Stack: 1, 19, 12  Output: 7 DFS in Action (Step 7) Enter recursively into the next child

34  Stack: 1, 19  Output: 7, 12 DFS in Action (Step 8) Return back from recursion and print the last visited node Print node 12

35  Stack: 1, 19, 31  Output: 7, 12 DFS in Action (Step 9) Enter recursively into the next child

36  Stack: 1, 19, 31, 21  Output: 7, 12 DFS in Action (Step 10) Enter recursively into the first child

37  Stack: 1, 19, 31, 21, 14  Output: 7, 12 DFS in Action (Step 11) Enter recursively into the first child

38  Stack: 1, 19, 31, 21, 14, 6  Output: 7, 12 DFS in Action (Step 12) Enter recursively into the first child

39  Stack: 1, 19, 31, 21, 14  Output: 7, 12, 6 DFS in Action (Step 13) Return back from recursion and print the last visited node Print node 6

40  Stack: 1, 19, 31, 21, 14, 23  Output: 7, 12, 6 DFS in Action (Step 14) Enter recursively into the next child 23

41  Stack: 1, 19, 31, 21, 14, 23, 21  Output: 7, 12, 6 DFS in Action (Step 15) Enter recursively into the first child 21

42  Stack: 1, 19, 31, 21, 14, 23  Output: 7, 12, 6 DFS in Action (Step 16) Node 21 already visited  return back

43  Stack: 1, 19, 31, 21, 14  Output: 7, 12, 6, 23 DFS in Action (Step 17) Return back from recursion and print the last visited node Print node 23

44  Stack: 1, 19, 31, 21  Output: 7, 12, 6, 23, 14 DFS in Action (Step 18) Return back from recursion and print the last visited node Print node 14

45  Stack: 1, 19, 31  Output: 7, 12, 6, 23, 14, 21 DFS in Action (Step 19) Return back from recursion and print the last visited node Print node 21

46  Stack: 1, 19  Output: 7, 12, 6, 23, 14, 21, 31 DFS in Action (Step 20) Return back from recursion and print the last visited node Print node 31

47  Stack: 1, 19, 21  Output: 7, 12, 6, 23, 14, 21, 31 DFS in Action (Step 21) Enter recursively into the next child 21

48  Stack: 1, 19  Output: 7, 12, 6, 23, 14, 21, 31 DFS in Action (Step 22) Node 21 already visited  return back

49  Stack: 1  Output: 7, 12, 6, 23, 14, 21, 31, 19 DFS in Action (Step 23) Return back from recursion and print the last visited node Print node 19

50  Stack: 1, 21  Output: 7, 12, 6, 23, 14, 21, 31, 19 DFS in Action (Step 24) Enter recursively into the next child 21

51  Stack: 1  Output: 7, 12, 6, 23, 14, 21, 31, 19 DFS in Action (Step 25) Node 21 already visited  return back

52  Stack: 1, 14  Output: 7, 12, 6, 23, 14, 21, 31, 19 DFS in Action (Step 26) Enter recursively into the next child 14

53  Stack: 1  Output: 7, 12, 6, 23, 14, 21, 31, 19 DFS in Action (Step 27) Node 14 already visited  return back

54  Stack: (empty)  Output: 7, 12, 6, 23, 14, 21, 31, 19, 1 DFS in Action (Step 28) DFS traversal completed Return back from recursion and print the last visited node Print node 1

Recursive DFS Graph Traversal Live Demo Ruse Plovdiv Bourgas Sofia Stara Zagora Pleven Varna

DFS for Graph Traversals Live Coding (Lab)

57  Breadth-First Search (BFS) first visits the neighbor nodes, then the neighbors of neighbors, then their neighbors, etc. Breadth-First Search (BFS) BFS (node) { queue  node queue  node visited[node] = true visited[node] = true while queue not empty while queue not empty v  queue v  queue print v print v for each child c of v for each child c of v if not visited[c] if not visited[c] queue  c queue  c visited[c] = true visited[c] = true}

58  Queue: 7  Output: BFS in Action (Step 1) Initially enqueue the root node

59  Queue: 7  Output: 7 BFS in Action (Step 2) Remove from the queue the next node and print it Print node 7

60  Queue: 7, 19  Output: 7 BFS in Action (Step 3) Enqueue all children of the current node Enqueue the first child 19

61  Queue: 7, 19, 21  Output: 7 BFS in Action (Step 4) Enqueue all children of the current node Enqueue the second child 21

62  Queue: 7, 19, 21, 14  Output: 7 BFS in Action (Step 5) Enqueue all children of the current node Enqueue the third child 14

63  Queue: 7, 19, 21, 14  Output: 7, 19 BFS in Action (Step 6) Remove from the queue the next node and print it Print node 19

64  Queue: 7, 19, 21, 14, 1  Output: 7, 19 BFS in Action (Step 7) Enqueue all children of the current node Enqueue the first child 1

65  Queue: 7, 19, 21, 14, 1, 12  Output: 7, 19 BFS in Action (Step 8) Enqueue all children of the current node Enqueue the second child 12

66  Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19 BFS in Action (Step 9) Enqueue all children of the current node Enqueue the third child 31

67  Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19 BFS in Action (Step 10) Child node 21 already visited  skip it

68  Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19, 21 BFS in Action (Step 11) Remove from the queue the next node and print it Print node 21

 Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19, BFS in Action (Step 12) Child node 14 already visited  skip it

 Queue: 7, 19, 21, 14, 1, 12, 31  Output: 7, 19, 21, BFS in Action (Step 13) Remove from the queue the next node and print it Print node 14

71  Queue: 7, 19, 21, 14, 1, 12, 31, 23  Output: 7, 19, 21, 14 BFS in Action (Step 14) Enqueue all children of the current node Enqueue the first child 23

72  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14 BFS in Action (Step 15) Enqueue all children of the current node Enqueue the second child 6

73  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1 BFS in Action (Step 16) Remove from the queue the next node and print it Print node 1

74  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1 BFS in Action (Step 17) Child node 7 already visited  skip it

75  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12 BFS in Action (Step 18) Remove from the queue the next node and print it No child nodes to enqueue Print node 12

76  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31 BFS in Action (Step 19) Remove from the queue the next node and print it Print node 31

77  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31 BFS in Action (Step 20) Child node 21 already visited  skip it

78  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23 BFS in Action (Step 21) Remove from the queue the next node and print it Print node 23

79  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23 BFS in Action (Step 22) Child node 21 already visited  skip it

80  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23, 6 BFS in Action (Step 23) Remove from the queue the next node and print it No child nodes to enqueue Print node 6

81  Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6  Output: 7, 19, 21, 14, 1, 12, 31, 23, 6 BFS in Action (Step 24) The queue is empty  stop BFS traversal completed

BFS Graph Traversal Live Demo Ruse Plovdiv Bourgas Sofia Stara Zagora Pleven Varna

83  What will happen if in the Breadth-First Search (BFS) algorithm we change the queue with a stack?  An iterative stack-based Depth-First Search (DFS) Iterative DFS and BFS BFS (node) { queue  node queue  node visited[node] = true visited[node] = true while queue not empty while queue not empty v  queue v  queue print v print v for each child c of v for each child c of v if not visited[c] if not visited[c] queue  c queue  c visited[c] = true visited[c] = true} DFS (node) { stack  node stack  node visited[node] = true visited[node] = true while stack not empty while stack not empty v  stack v  stack print v print v for each child c of v for each child c of v if not visited[c] if not visited[c] stack  c stack  c visited[c] = true visited[c] = true}

Iterative DFS Graph Traversal Live Demo Ruse Plovdiv Bourgas Sofia Stara Zagora Pleven Varna

Graph Connectivity Finding the Connected Components

86  Connected component of undirected graph  A sub-graph in which any two nodes are connected to each other by paths  E.g. the graph on the right consists of 3 connected components Graph Connectivity

87  Finding the connected components in a graph  Loop through all nodes and start a DFS / BFS traversing from any unvisited node  Each time you start a new traversal  You find a new connected component Finding All Graph Connected Components

88 Graph Connected Components: Algorithm visited[] = false; foreach node from graph G { if (not visited[node]) if (not visited[node]) { DFS(node); DFS(node); countOfComponents++; countOfComponents++; }} F D A G C H DFS (node) { if (not visited[node]) if (not visited[node]) { visited[node] = true; visited[node] = true; foreach c in node.children foreach c in node.children DFS(c); DFS(c); }} N J K M L I Q P E B O

Finding the Connected Components in an Undirected Graph Live Demo F D A G C H N J K M L I Q P E B O

Topological Sorting Ordering a Graph by Set of Dependencies

91  Topological sorting (ordering) of a directed graph  Linear ordering of its vertices, such that  For every directed edge from vertex u to vertex v, u comes before v in the ordering  Example:  7 → 5 → 3 → 11 → 8 → 2 → 9 → 10  3 → 5 → 7 → 8 → 11 → 2 → 9 → 10  5 → 7 → 3 → 8 → 11 → 10 → 9 → 2 Topological Sorting

92  We have a set of learning topics with dependencies  Order the topics in such order that all dependencies are met  Example:  Loops depend on variables, conditionals and IDEs  Variables depend on IDEs  Bits depend on variables and loops  Conditionals depend on variables  Ordering:  IDEs  variables  loops  bits Topological Sorting – Example variables IDEs conditionals loops bits

93  Rules  Undirected graphs cannot be sorted  Graphs with cycles cannot be sorted  Sorting is not unique  Various sorting algorithms exists and they give different results Topological Sorting – Rules

94  Source removal top-sort algorithm: 1. Create an empty list Repeat until the graph is empty: 2. Find a node without incoming edges 3. Print this node 4. Remove the edge from the graph Topological Sorting: Source Removal Algorithm

95 Source Removal Algorithm L ← empty list that will hold the sorted elements (output) S ← set of all nodes with no incoming edges while S is non-empty do remove some node n from S remove some node n from S append n to L append n to L for each node m with an edge e: { n  m } for each node m with an edge e: { n  m } remove edge e from the graph remove edge e from the graph if m has no other incoming edges then if m has no other incoming edges then insert m into S insert m into S if graph is empty return L (a topologically sorted order) return L (a topologically sorted order)else return "Error: graph has at least one cycle" return "Error: graph has at least one cycle"

96 Step #1: Find a Node with No Incoming Edges A C D B F E The node A is the only node without incoming edges L

A C D B F E 97 Step #2: Remove Node A with Its Edges A L

98 Step #3: Find a Node with No Incoming Edges C D B F E The node B is the only node without incoming edges L A

C D B F E 99 Step #4: Remove Node B with Its Edges A L B

C D F E 100 Step #5: Find a Node with No Incoming Edges A L B The node E is the only node without incoming edges

C D F E 101 Step #6: Remove Node E with Its Edges A L BE

C D F 102 Step #7: Find a Node with No Incoming Edges L The node D is the only node without incoming edges ABE

C D F 103 Step #8: Remove Node D with Its Edges A L BED

CF 104 Step #9: Find a Node with No Incoming Edges L The node C is the only node without incoming edges ABED

CF 105 Step #10: Remove Node C with Its Edges A L BEDC

F 106 Step #11: Find a Node with No Incoming Edges L The node F is the only node without incoming edges ABEDC

F 107 Step #12: Remove Node F with Its Edges A L BEDC F

108 Result: Topological Sorting ACDBFE

Topological Sorting: Source Removal Algorithm Live Demo

110 Topological Sorting: DFS Algorithm sortedNodes = { } // linked list to hold the result visitedNodes = { } // set of already visited nodes foreach node in graphNodes TopSortDFS(node) TopSortDFS(node) TopSortDFS(node) if node ∉ visitedNodes if node ∉ visitedNodes visitedNodes ← node visitedNodes ← node for each child c of node for each child c of node TopSortDFS(c) TopSortDFS(c) insert node upfront in the sortedNodes insert node upfront in the sortedNodes Visualization:

111 TopSort: DFS Algorithm + Cycle Detection sortedNodes = { } // linked list to hold the result visitedNodes = { } // set of already visited nodes cycleNodes = { } // set of nodes in the current DFS cycle foreach node in graphNodes TopSortDFS(node) TopSortDFS(node) TopSortDFS(node) if node ϵ cycleNodes if node ϵ cycleNodes return "Error: cycle detected" return "Error: cycle detected" if node ∉ visitedNodes if node ∉ visitedNodes visitedNodes ← node visitedNodes ← node cycleNodes ← node cycleNodes ← node for each child c of node for each child c of node TopSortDFS(c) TopSortDFS(c) remove node from cycleNodes remove node from cycleNodes insert node upfront in the sortedNodes insert node upfront in the sortedNodes

Topological Sorting Live Coding (Lab)

113  Representing graphs in memory  Adjacency list holding the children for each node  Adjacency matrix  List of edges  Numbering the nodes for faster access  Depth-First Search (DFS) – recursive in-depth traversal  Breadth-First Search (BFS) – in-width traversal with a queue  Topological sorting – source removal algorithms and DFS algorithm Summary

? ? ? ? ? ? ? ? ? Graphs and Graph Algorithms

License  This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 115  Attribution: this work may contain portions from  "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with C#CC-BY-SA  "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA licenseData Structures and AlgorithmsCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg