CS Dudes
meetup #1. Graph theory basics
1
Snakes & Ladders
2
Brute-force solution
Recursively inspect every possible combination of moves O(n!)
Introduce memo table to improve performance
Invent DP version
3
GRAPH is a structure
amounting to a set of objects in
which some pairs of the objects
are in some sense "related"
4
Graph types
Directed/undirected
Weighted/unweighted
Cyclic/acyclic
Connected/disconnected
5
Tree - acyclic connected
undirected graph
6
Applications
Routes
Optimizations
Scheduling
Fuzzy search
Performance improvements
Classifications
7
Representation in CS
8
Adjacency matrix
A
E
B
D
C
A B C D E
A 0 1 0 0 1
B 1 0 1 1 1
C 0 1 0 1 0
D 0 1 1 0 1
E 1 1 0 1 0
9
Adjacency list
A
E
B
D
C
A B E
B A C D E
C B D
D B C E
E A B D
10
Ways to find routes
11
One source and One Destination
A* Search Algorithm
(For Unweighted as well as Weighted Graphs)
12
One Source, All Destination
BFS (For Unweighted Graphs)
Dijkstra (For Weighted Graphs without negative weights)
Bellman Ford (For Weighted Graphs with negative weights)
13
Between every pair of nodes
Floyd-Warshall
Johnson’s Algorithm
14
BFS in action
15
BFS in action. Shortest routes from A
A
E
B
D
C
F
A
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
16
BFS in action. Shortest routes from A
A
E
B
D
C
F
A
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
1
1
B
E
0
17
BFS in action. Shortest routes from A
A
E
B
D
C
F
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
1
1
B
E
0
C
D
2
2
18
BFS in action. Shortest routes from A
A
E
B
D
C
F
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
1
1
E
0
C
D
2
2
19
BFS in action. Shortest routes from A
A
E
B
D
C
F
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
1
1
0 C
D2
2
F
3
20
BFS in action. Shortest routes from A
A
E
B
D
C
F
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
1
1
0 D
2
2
F
3
21
BFS in action. Shortest routes from A
A
E
B
D
C
F
Inspection queue:
A B E
B A C D E
C B D F
D B C E
E A B D
F C
1
1
0 F
2
2
3
22
Implementation
23
Snakes & Ladders
24
public class Main {
public static void main( String[] args ) {
Board board = new Board( 6, 5 );
board.addLadder( 3, 22 );
board.addLadder( 5, 8 );
board.addLadder( 11, 26 );
board.addLadder( 20, 29 );
board.addSnake( 17, 4 );
board.addSnake( 19, 7 );
board.addSnake( 21, 9 );
board.addSnake( 27, 1 );
MinDiceSolver solver = new MinDiceSolver( board );
System.out.println(
"Min dice rolls number is " + solver.getMinDiceRolls()
);
}
} 25
public class Board {
private int[] paths;
public Board( int width, int height ) {
paths = new int[width * height];
for ( int i = 0; i < paths.length; i++ ) {
paths[i] = i + 1;
}
}
public void addLadder( int from, int to ) { paths[from - 1] = to - 1; }
public void addSnake( int from, int to ) { paths[from - 1] = to - 1; }
}
26
public class MinDiceSolver {
private static final int DICE_SIZE = 6;
private final Board board;
public MinDiceSolver( Board board ) { this.board = board; }
private static class QEntry {
private int cellid;
private int distance;
public QEntry( int cellid, int distance ) {
this.cellid = cellid;
this.distance = distance;
}
}
public int getMinDiceRolls() { ... } // <!-- need to implement
}
27
public int getMinDiceRolls() {
int[] paths = board.getPaths();
boolean[] visited = new boolean[ paths.length ];
QEntry current = new QEntry( 0, 0 );
visited[0] = true;
Queue<QEntry> q = new LinkedList<>();
q.add( current );
while ( !q.isEmpty() ) {
current = q.poll();
if ( current.cellid == paths.length - 1 ) { break; }
for ( int i = current.cellid + 1; ( i <= current.cellid + DICE_SIZE ) && i < paths.length; i++ ) {
if ( !visited[i] ) {
q.add( new QEntry( paths[i], current.distance + 1 ) );
visited[i] = true;
}
}
}
return current.distance;
}
28
Gimme the code!
Grab & run:
$ git clone https://gitlab.com/csdudes/meetup1.git
$ cd metup1/
$ ./gradlew run
29

Graph theory basics

  • 1.
    CS Dudes meetup #1.Graph theory basics 1
  • 2.
  • 3.
    Brute-force solution Recursively inspectevery possible combination of moves O(n!) Introduce memo table to improve performance Invent DP version 3
  • 4.
    GRAPH is astructure amounting to a set of objects in which some pairs of the objects are in some sense "related" 4
  • 5.
  • 6.
    Tree - acyclicconnected undirected graph 6
  • 7.
  • 8.
  • 9.
    Adjacency matrix A E B D C A BC D E A 0 1 0 0 1 B 1 0 1 1 1 C 0 1 0 1 0 D 0 1 1 0 1 E 1 1 0 1 0 9
  • 10.
    Adjacency list A E B D C A BE B A C D E C B D D B C E E A B D 10
  • 11.
    Ways to findroutes 11
  • 12.
    One source andOne Destination A* Search Algorithm (For Unweighted as well as Weighted Graphs) 12
  • 13.
    One Source, AllDestination BFS (For Unweighted Graphs) Dijkstra (For Weighted Graphs without negative weights) Bellman Ford (For Weighted Graphs with negative weights) 13
  • 14.
    Between every pairof nodes Floyd-Warshall Johnson’s Algorithm 14
  • 15.
  • 16.
    BFS in action.Shortest routes from A A E B D C F A Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 16
  • 17.
    BFS in action.Shortest routes from A A E B D C F A Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 1 1 B E 0 17
  • 18.
    BFS in action.Shortest routes from A A E B D C F Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 1 1 B E 0 C D 2 2 18
  • 19.
    BFS in action.Shortest routes from A A E B D C F Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 1 1 E 0 C D 2 2 19
  • 20.
    BFS in action.Shortest routes from A A E B D C F Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 1 1 0 C D2 2 F 3 20
  • 21.
    BFS in action.Shortest routes from A A E B D C F Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 1 1 0 D 2 2 F 3 21
  • 22.
    BFS in action.Shortest routes from A A E B D C F Inspection queue: A B E B A C D E C B D F D B C E E A B D F C 1 1 0 F 2 2 3 22
  • 23.
  • 24.
  • 25.
    public class Main{ public static void main( String[] args ) { Board board = new Board( 6, 5 ); board.addLadder( 3, 22 ); board.addLadder( 5, 8 ); board.addLadder( 11, 26 ); board.addLadder( 20, 29 ); board.addSnake( 17, 4 ); board.addSnake( 19, 7 ); board.addSnake( 21, 9 ); board.addSnake( 27, 1 ); MinDiceSolver solver = new MinDiceSolver( board ); System.out.println( "Min dice rolls number is " + solver.getMinDiceRolls() ); } } 25
  • 26.
    public class Board{ private int[] paths; public Board( int width, int height ) { paths = new int[width * height]; for ( int i = 0; i < paths.length; i++ ) { paths[i] = i + 1; } } public void addLadder( int from, int to ) { paths[from - 1] = to - 1; } public void addSnake( int from, int to ) { paths[from - 1] = to - 1; } } 26
  • 27.
    public class MinDiceSolver{ private static final int DICE_SIZE = 6; private final Board board; public MinDiceSolver( Board board ) { this.board = board; } private static class QEntry { private int cellid; private int distance; public QEntry( int cellid, int distance ) { this.cellid = cellid; this.distance = distance; } } public int getMinDiceRolls() { ... } // <!-- need to implement } 27
  • 28.
    public int getMinDiceRolls(){ int[] paths = board.getPaths(); boolean[] visited = new boolean[ paths.length ]; QEntry current = new QEntry( 0, 0 ); visited[0] = true; Queue<QEntry> q = new LinkedList<>(); q.add( current ); while ( !q.isEmpty() ) { current = q.poll(); if ( current.cellid == paths.length - 1 ) { break; } for ( int i = current.cellid + 1; ( i <= current.cellid + DICE_SIZE ) && i < paths.length; i++ ) { if ( !visited[i] ) { q.add( new QEntry( paths[i], current.distance + 1 ) ); visited[i] = true; } } } return current.distance; } 28
  • 29.
    Gimme the code! Grab& run: $ git clone https://gitlab.com/csdudes/meetup1.git $ cd metup1/ $ ./gradlew run 29