# Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.

## Presentation on theme: "Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers."— Presentation transcript:

Graphs By JJ Shepherd

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Download ppt "Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers."

Similar presentations