• Aucun résultat trouvé

Charles Lin Graph

N/A
N/A
Protected

Academic year: 2022

Partager "Charles Lin Graph"

Copied!
105
0
0

Texte intégral

(1)

Graph

Charles Lin

(2)

Agenda

Graph Representation

DFS

BFS

Dijkstra

A* Search

Bellman-Ford

Floyd-Warshall

Iterative? Non-iterative?

MST

Flow – Edmond-Karp

(3)

Graph Representation

Adjacency Matrix

bool way[100][100];

cin >> i >> j;

way[i][j] = true;

(4)

Graph Representation

Adjacency Linked List

vector<int> v[100];

cin >> i >> j;

v[i].push_back(j);

(5)

Graph Representation

Edge List

struct Edge {

int Start_Vertex, End_Vertex;

} edge[10000];

cin >> edge[i].Start_Vertex

>> edge[i].End_Vertex;

(6)

Graph Representation

(7)

Graph Theory

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(8)

Graph Theory

Mission: To go from Point A to Point E

How?

(9)

Depth First Search (DFS)

Structure to use: Stack

(10)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(11)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(12)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(13)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(14)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(15)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

B is visited

already

(16)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(17)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(18)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(19)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

(20)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

We find a path from Point A to Point E, but …

(21)

Depth First Search (DFS)

stack<int> s;

bool Visited[MAX];

void DFS(int u) { s.push(u);

if (u == GOAL) {

for (int x = 0; x < s.size(); x++) printf(“%d “, s[x]);

return ; }

for (int x = 0; x < MAX; x++) if (!Visited[x]) {

Visited[x] = true;

DFS(x);

Visited[x] = false;

} s.pop();

}

int main() {

memset(Visited, 0, sizeof(Visited));

// Input Handling (GOAL = ?) DFS(0);

}

Very Important

It needs to be restored for other points to

traverse, or the path may not be found

(22)

Depth First Search (DFS)

Usage:

Finding a path from start to destination

(NOT RECOMMENDED – TLE)Topological Sort (T-Sort)

Strongly-Connected

Components (SCC)

Detect cycles

(23)

Depth First Search (DFS)

Topological Sort

Directed Acyclic Graph (DAG)

Find the order of nodes such that for each node, every parent is before that node on the list

Dumb method: Check for root of residual graph (Do it n times)

Better Method: Reverse of finishing time

Start from all vertices!

(24)

Depth First Search (DFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Cycle Exists !!!

T-Sort:

(25)

Depth First Search (DFS)

A

B

C

D

E

OK

T-Sort:

(26)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack:

NONE

T-Sort:

(27)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack:

NONE

T-Sort:

(28)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack:

NONE

T-Sort:

(29)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack:

NONE

T-Sort:

(30)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E

T-Sort:

(31)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D

T-Sort:

(32)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B

T-Sort:

(33)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B

T-Sort:

(34)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B

T-Sort:

(35)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B

T-Sort:

(36)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B

T-Sort:

(37)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B, C

T-Sort:

(38)

Depth First Search (DFS)

A

B

C

D

E

Things in output stack: E, D, B, C, A

T-Sort Output: A, C, B, D, E

T-Sort:

(39)

Depth First Search (DFS)

Strongly Connected Components

Directed Graph

Find groups of nodes such that in each group, every node has

to every other node

a path

(40)

Depth First Search (DFS)

Strongly Connected Components

Method: DFS twice (Kosaraju)

Do DFS on graph G from all vertices, record finishing time of node

Reverse direction of edges

Do DFS on Graph G from all vertices

sorted by decreasing finishing time

Each DFS tree is an SCC

(41)

Depth First Search (DFS)

Strongly Connected Components

Note: After grouping the vertices,

the new nodes form a Directed

Acyclic Graph

(42)

Breadth First Search (BFS)

Structure to use: Queue

(43)

Breadth First Search (BFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in queue:

A, C 3 A, B 10

(44)

Breadth First Search (BFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in queue:

A, B 10 A, C, E 5 A, C, B 7 A, C, D 8

(45)

Breadth First Search (BFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in queue:

A, C, E 5 A, C, B 7 A, C, D 8 A, B, C 11 A, B, D 12

(46)

Breadth First Search (BFS)

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in queue:

A, C, B 7 A, C, D 8 A, B, C 11 A, B, D 12

We are done !!!

(47)

Breadth First Search (BFS)

Application:

Finding shortest path

Flood-Fill

(48)

Dijkstra

Structure to use: Priority Queue

Consider the past

The length of path visited so far

No negative edges allowed

Maintain known vertices

At each step:

Take the unknown vertex with

smallest overall estimate

(49)

Dijkstra

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in priority queue:

A, C 3 A, B 10

Current Route:

A

(50)

Dijkstra

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in priority queue:

A, C, E 5 A, C, B 7 A, B 10 A, C, D 11

Current Route:

A, C

(51)

Dijkstra

A

B

C

D

E 10

3

1 4

2 2

8 7 9

Things in priority queue:

A, C, B 7 A, B 10 A, C, D 11

We find the shortest path already !!!

Current Route:

A, C, E

(52)

A* Search

Structure to use: Priority Queue

Consider the past + future

How to consider the future?

Admissible Heuristic

(Best-Case Prediction:

must never overestimate)

How to predict?

(53)

A* Search

Prediction 1: Displacement

Given coordinates, calculate the displacement of current node to destination

sqrt((x2 – x1)

2

+ (y2 – y1)

2

)

Prediction 2: Manhattan Distance

Given grid, calculate the

horizontal and vertical movement

(x2 – x1) + (y2 – y1)

(54)

A* Search

Dest

Start

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(55)

A* Search

Dest

1 + 5 = 6

Start (0 + 6 = 6) 1 + 5 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(56)

A* Search

Dest 2 + 4 = 6

1 + 5 = 6

Start (0 + 6 = 6) 1 + 5 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(57)

A* Search

3 + 3 = 6 Dest

2 + 4 = 6 1 + 5 = 6

Start (0 + 6 = 6) 1 + 5 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(58)

A* Search

4 + 4 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6 1 + 5 = 6

Start (0 + 6 = 6) 1 + 5 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(59)

A* Search

4 + 4 = 8 5 + 3 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6 1 + 5 = 6

Start (0 + 6 = 6) 1 + 5 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(60)

A* Search

4 + 4 = 8 5 + 3 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6 1 + 5 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 - Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(61)

A* Search

4 + 4 = 8 5 + 3 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(62)

A* Search

4 + 4 = 8 5 + 3 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(63)

A* Search

4 + 4 = 8 5 + 3 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6 4 + 4 = 8

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(64)

A* Search

4 + 4 = 8 5 + 3 = 8 6 + 2 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6 4 + 4 = 8

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(65)

A* Search

4 + 4 = 8 5 + 3 = 8 6 + 2 = 8 7 + 1 = 8

3 + 3 = 6 4 + 2 = 6 Dest

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6 4 + 4 = 8

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(66)

A* Search

4 + 4 = 8 5 + 3 = 8 6 + 2 = 8 7 + 1 = 8 8 + 2 = 10 3 + 3 = 6 4 + 2 = 6 Dest (8 + 0 = 8)

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6 4 + 4 = 8

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(67)

A* Search

4 + 4 = 8 5 + 3 = 8 6 + 2 = 8 7 + 1 = 8 8 + 2 = 10 3 + 3 = 6 4 + 2 = 6 Dest (8 + 0 = 8)

2 + 4 = 6

1 + 5 = 6 3 + 3 = 6

Start (0 + 6 = 6) 1 + 5 = 6 2 + 4 = 6 3 + 3 = 6 4 + 4 = 8

- Manhattan Distance is used

- If there is a tie, consider the one with lowest heuristic first

- If there is still a tie, consider in Up, Down, Left, Right order

(68)

A* Search

A* Search = Past + Future

A* Search – Future = ? Dijkstra

A* Search – Past = ?

Greedy

(69)

Graph Theory

How about graph with negative edges?

Bellman-Ford

(70)

Bellman-Ford Algorithm

Consider the source as weight 0, others as infinity

Count = 0, Improve = true

While (Count < n and Improve) {

Improve = falseCount++

For each edge uv

If u.weight + uv distance < v.weight {

Improve = true

v.weight = u.weight + uv.distance

}

}

If (Count == n)

print “Negative weight cycle detected”

Else print shortest path distance

(71)

Bellman-Ford Algorithm

Time Compleity: O(VE)

(72)

Graph Theory

All we have just dealt with is single source

How about multiple sources (all sources)?

Floyd-Warshall Algorithm

(73)

Floyd-Warshall Algorithm

•• 1. Compute on subgraph {1}

2. Compute on subgraph {1 ,2}

3. Compute on subgraph {1, ..., 3}

N. Compute on entire graph

Done !

4. ...

(74)

Floyd-Warshall Algorithm

For (int k = 0; k < MAX; k++) {For (int i = 0; i < MAX; i++) {

For (int j = 0; j < MAX; j++) {Path[i][j] = min(Path[i][j],

Path[i][k] + Path[k][j]);

}}

}

(75)

Iterative? Non-iterative?

What is iterative-deepening?

Given limited time, f nd the current most optimal solution

Dijkstra (Shortest 1-step Solution)

Dijkstra (Shortest 2-steps Solution)

Dijkstra (Shortest 3-steps Solution)

Dijkstra (Shortest n-steps Solution)

Suitable in bi-directional search (Faster than

1-way search): only for DFS, tricky otherwise

(76)

s t

(77)

s t

(78)

Minimum Spanning Tree (MST)

Given an undirected graph, find the minimum cost so that every node has path to other nodes

Kruskal’s Algorithm

Prim’s Algorithm

(79)

Kruskal’s Algorithm

Continue Finding Shortest Edge

If no cycle is formed

add it into the list

Construct a parent-child relationship

If all nodes have the same root, we are done (path compression)

General idea: union-find, may be

useful in other situations

(80)

Kruskal’s Algorithm

Node A B C D E

Parent A B C D E

A

B

C

D

E 4

3

1

2

7 8

10

(81)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A B B D E

10

(82)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A B B B E

10

(83)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A A A A E

10

(84)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A A A A E

Cycle formed

10

(85)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A A A A E

10

(86)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A A A A E

Cycle formed

10

(87)

Kruskal’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

Edge List:

BC 1

Node A B C D E

Parent A A A A A

10

(88)

Kruskal’s Algorithm

Path Compression can be done when we find root

int find_root(int x) {

return (parent[x] == x)? x:

return (parent[x] = find_root(parent[x]));

}

(89)

Prim’s Algorithm

While not all nodes are visited

While the list is not empty and the minimum edge connects to visited node

Pop out the edge

If the list is empty, print Impossible and return

Else

Consider popped edge

Mark its endpoint as visited

Add all its unvisited edges to the list

Print the tree

(90)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

AC 3 AB 4

(91)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

CB 1 AB 4 CD 7 CE 10

(92)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

BD 2 AB 4 CD 7 CE 10

(93)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

AB 4 CD 7 DE 8 CE 10

(94)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

CD 7 DE 8 CE 10

Cycle

formed

(95)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

DE 8 CE 10

Cycle

formed

(96)

Prim’s Algorithm

A

B

C

D

E 4

3

1

2

7 8

10

Edge List:

CE 10

We are done

(97)

Directed Minimum Spanning Tree

We have just dealt with undirected MST.

How about directed?

Using Kruskal’s or Prim’s cannot solve directed MST problem

How?

Chu-Liu/Edmonds Algorithm

(98)

Chu-Liu/Edmonds Algorithm

For each vertex, find the smallest incoming edge

While there is cycle

Find an edge entering the cycle with smallest increase

Remove the edge pointing to the same node and replace by new edge

Print out the tree

(99)

Chu-Liu/Edmonds Algorithm

A

B

C

D

E 10

3

5 2

8 4

2 7 9

(100)

Chu-Liu/Edmonds Algorithm

Cycle formed (B, C, D) A => B, C, D = 10 – 2 = 8

A => B, C, D = 3 – 2 = 1 E => B, C, D = 9 – 4 = 5 A

B

C

D

E 10

3

5 2

8 4

2 7 9

(101)

Chu-Liu/Edmonds Algorithm

No cycle formed, we terminate A

B

C

D

E 10

3

5 2

8 4

2 7 9

(102)

Chu-Liu/Edmonds Algorithm

Using this algorithm may unloop a cycle and create another cycle…

… But since the length of tree is increasing, there will have an end eventually

Worst Case is doing n-1 times

Time Complexity is O(EV)

(103)

Flow

A graph with weight (capacity)

Mission: Find out the maximum flow from source to destination

How?

(104)

Edmond-Karp

Do {

Do BFS on the graph to find maximum flow in the graph

Add it to the total flow

For each edge in the maximum flowDeduct the flow just passed

} While the flow is not zero;

(105)

The End

Références

Documents relatifs

We then introduce the graph path ordering inspired by the recursive path ordering on terms and show that it is a well-founded rewrite ordering on graphs for which checking

Further an edge links two nodes of the line graph if and only if the corresponding edges of the original graph share a node.. Although line graph is the term most commonly used, it

These isolates showed unique internal transcribed spacer sequences, different enough from those of any described species to justify new species status.. These three distinct

Patient preference for subcutaneous trastuzumab via handheld syringe versus intravenous infusion in HER2-positive early breast cancer: cohort 2 of the PrefHer study; abstract

In this section, we will present an algorithm which is capable of solving our optimization problem, stated as follows: given a directed hypergraph H representing all the

Given a graph and a goal query, we start with an empty sample and we continuously select a node that we ask the user to label, until the learned query selects exactly the same set

The user is interactively asked to visualize small fragments of the graph and to label nodes of interest as positive or negative, depending on whether or not she would like the nodes

This study examines marital and parental status in relation to perceptions of the quality of work and family roles (psychological well-being, job satisfaction, work involvement,