Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Part 2 Adjacency Matrix

Similar presentations


Presentation on theme: "Graphs Part 2 Adjacency Matrix"— Presentation transcript:

1 Graphs Part 2 Adjacency Matrix
By JJ Shepherd

2 Adjacency Matrix Graphs can also be represented by an adjacency matrix
For each edge the weight is stored in a 2D matrix where the vertices intersect V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7

3 Depth First Search Method for visiting vertices in a graph
The idea is go as deep as possible until there is a dead end No unvisited vertices left No remaining no edges

4 Depth First Search Start from an origin or arbitrarily picked vertex
Add that vertex to the marked list Traverse an edge to the next vertex If that vertex is in the marked list then return to the previous vertex. Repeat steps 2-4 until there are vertices that have not be visited

5 Depth First Search We will start from v1 and we pick edges based on vertex ascending order V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7

6 Depth First Search V1 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1

7 Depth First Search V2 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2

8 Depth First Search V4 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4

9 Depth First Search V3 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3

10 Depth First Search V1 is already in the marked vertices so return to v3 and go to its next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3

11 Depth First Search V5 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5

12 Depth First Search V6 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 v6

13 Depth First Search V6 has no outgoing edges so return to v5 and go to the next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 v6

14 Depth First Search V7 is added to the marked vertices list traverse to next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 v6 v7

15 Depth First Search V7 has no outgoing edges return to v5, now that all of the vertices have been visited the algorithm ends V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 v6 v7

16 Breadth First Search Similar to DFS but each child is visited before going deeper Start from the origin or an arbitrary vertex and access its value Add that vertex to the marked list Look at every one of its neighbors Access the values if it’s not in the visited or marked list, and add those vertices to the visited list Otherwise continue on Traverse to the next node in the neighbor list Repeat steps 2 -4 for each neighbor until there are no unmarked vertices left and only accessing values if they are not in the visited list

17 Breadth First Search We will start from v1 and we pick edges based on vertex ascending order V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7

18 Breadth First Search Access the value of v1 and mark vertex v1 V1 V2
1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 Visited vertices

19 Breadth First Search Access the values of its neighbors and add those vertices to the visited list. Then traverse to the next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 Visited vertices v2 v4

20 Breadth First Search Mark vertex v2 V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4
1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 Visited vertices v4

21 Breadth First Search Access the values of its neighbors. V4 has already been visited so it returns back to v1 and moves along V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 Visited vertices v4

22 Breadth First Search Mark vertex v4 V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4
1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 Visited vertices

23 Breadth First Search Access the values of its neighbors v3 and v5 and add those to the visited list then traverse to the next vertex V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 Visited vertices v3 v5

24 Breadth First Search Mark v3 V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6
1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 Visited vertices v5

25 Breadth First Search Access the values of its neighbors. However v1 has been marked and v5 has been visited but v6 has not so add that to the visited list. V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 Visited vertices v5 v6

26 Breadth First Search Mark v5. V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6
1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 Visited vertices v6

27 Breadth First Search Visit the nodes. V6 has already been visited by v7 has not so it’s added to the visited list V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 Visited vertices v6 v7

28 Breadth First Search Mark v6. It has not neighbors so back to v5 V1 V2
1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 v6 Visited vertices v7

29 Breadth First Search Mark v7. It has not neighbors and all nodes have been marked so end. V1 V2 V3 V4 V5 V6 V7 1 v1 v2 v3 v4 v5 v6 v7 Marked vertices v1 v2 v4 v3 v5 v6 v7 Visited vertices


Download ppt "Graphs Part 2 Adjacency Matrix"

Similar presentations


Ads by Google