Download presentation

Presentation is loading. Please wait.

Published byGodwin Baker Modified over 4 years ago

1
Graphs By JJ Shepherd

2
Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers than trees – Following a graph could be open ended because of cycles – Infinite loops

3
Introduction Graphs are a set of Vertices and Edges G = (V,E) Where V = {v i }; And E = {e k = } Graphs can be directed or undirected – Directed is one way – Undirected is bi-directional

4
Introduction Edges may have weights Thinks of a map – Cities are vertices – Edge weights are the distance between them Finding shortest paths are a tradition problem that would use graphs Two ways to represent to represent a graph – Linked Structure – Adjacency Matrix

5
Linked Structure Each vertex object has an unique name and a list of edges to other vertices Edges connect the vertices and have an associated weight

6
Linked Structure Here’s a graph v1 v2v3 v4 v5 v6 v7

7
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

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

9
Depth First Search We will start from v1 and we pick edges based on vertex ascending order v1 v2v3 v4 v5 v6 v7

10
Depth First Search V1 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1

11
Depth First Search V2 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2

12
Depth First Search V4 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4

13
Depth First Search V3 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3

14
Depth First Search V1 is already in the marked vertices so return to v3 and go to its next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3

15
Depth First Search V5 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5

16
Depth First Search V6 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5v6

17
Depth First Search V6 has no outgoing edges so return to v5 and go to the next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5v6

18
Depth First Search V7 is added to the marked vertices list traverse to next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5v6v7

19
Depth First Search V7 has no outgoing edges return to v5, now that all of the vertices have been visited the algorithm ends v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5v6v7

20
Breadth First Search Similar to DFS but each child is visited before going deeper 1.Start from the origin or an arbitrary vertex and access its value 2.Add that vertex to the marked list 3.Look at every one of its neighbors a.Access the values if it’s not in the visited or marked list, and add those vertices to the visited list b.Otherwise continue on 4.Traverse to the next node in the neighbor list 5.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

21
Breadth First Search We will start from v1 and we pick edges based on vertex ascending order v1 v2v3 v4 v5 v6 v7

22
Breadth First Search Access the value of v1 and mark vertex v1 v1 v2v3 v4 v5 v6 v7 Marked vertices v1 Visited vertices v1

23
Breadth First Search Access the values of its neighbors and add those vertices to the visited list. Then traverse to the next vertex v1 v2v3 v4 v5 v6 v7 Marked vertices v1 Visited vertices v1v2v4

24
Breadth First Search Mark vertex v2 v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2 Visited vertices v1v2v4

25
Breadth First Search Access the values of its neighbors. V4 has already been visited so it returns back to v1 and moves along v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2 Visited vertices v1v2v4

26
Breadth First Search Mark vertex v4 v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4 Visited vertices v1v2v4

27
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 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4 Visited vertices v1v2v4v3v5

28
Breadth First Search Mark v3 v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3 Visited vertices v1v2v4v3v5

29
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 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3 Visited vertices v1v2v4v3v5v6

30
Breadth First Search Mark v5. v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3 Visited vertices v1v2v4v3v5v6

31
Breadth First Search Mark v5. v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5 Visited vertices v1v2v4v3v5v6

32
Breadth First Search Visit the nodes. V6 has already been visited by v7 has not so it’s added to the visited list v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5 Visited vertices v1v2v4v3v5v6v7

33
Breadth First Search Mark v6. It has not neighbors so back to v5 v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5v6 Visited vertices v1v2v4v3v5v6v7

34
Breadth First Search Mark v7. It has not neighbors and all nodes have been marked so end. v1 v2v3 v4 v5 v6 v7 Marked vertices v1v2v4v3v5v6v7 Visited vertices v1v2v4v3v5v6v7

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google