graphs by jj shepherd. introduction graphs are simply trees with looser restrictions – you can...
TRANSCRIPT
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 EdgesG = (V,E)
Where V = {vi};
And E = {ek = <vi,vj>}• 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
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 list3. Traverse an edge to the next vertex4. 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
v2 v3
v4 v5
v6 v7
Depth First Search
• V1 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1
Depth First Search
• V2 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2
Depth First Search
• V4 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4
Depth First Search
• V3 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3
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
Marked vertices
v1 v2 v4 v3
Depth First Search
• V5 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3 v5
Depth First Search
• V6 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3 v5 v6
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
Marked vertices
v1 v2 v4 v3 v5 v6
Depth First Search
• V7 is added to the marked vertices list traverse to next vertex
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3 v5 v6 v7
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
Marked vertices
v1 v2 v4 v3 v5 v6 v7
Breadth First Search
• Similar to DFS but each child is visited before going deeper1. Start from the origin or an arbitrary vertex and access its
value2. Add that vertex to the marked list3. 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 list5. 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
v2 v3
v4 v5
v6 v7
Breadth First Search
• Access the value of v1 and mark vertex v1
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1Visited 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
v2 v3
v4 v5
v6 v7
Marked vertices
v1Visited vertices
v1 v2 v4
Breadth First Search
• Mark vertex v2
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2Visited vertices
v1 v2 v4
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
Marked vertices
v1 v2Visited vertices
v1 v2 v4
Breadth First Search
• Mark vertex v4
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4Visited vertices
v1 v2 v4
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
Marked vertices
v1 v2 v4Visited vertices
v1 v2 v4 v3 v5
Breadth First Search
• Mark v3
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3Visited vertices
v1 v2 v4 v3 v5
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
Marked vertices
v1 v2 v4 v3Visited vertices
v1 v2 v4 v3 v5 v6
Breadth First Search
• Mark v5.
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3Visited vertices
v1 v2 v4 v3 v5 v6
Breadth First Search
• Mark v5.
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3 v5Visited vertices
v1 v2 v4 v3 v5 v6
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
Marked vertices
v1 v2 v4 v3 v5Visited vertices
v1 v2 v4 v3 v5 v6 v7
Breadth First Search
• Mark v6. It has not neighbors so back to v5
v1
v2 v3
v4 v5
v6 v7
Marked vertices
v1 v2 v4 v3 v5 v6Visited vertices
v1 v2 v4 v3 v5 v6 v7