140 likes | 158 Views
Learn about different ways to represent graphs, node degrees, storing graphs, BFS, DFS, and more in a straightforward manner. Master the fundamentals of graphs for efficient problem-solving in computer science.
E N D
Graph representations • Nodes or vertices • Edges • Different types of graphs • Weighted vs. unweighted • Undirected vs. directed • Multiple edges (from one node to another) or not • Self-loops (edge going to same node) or not
Degree • Vertex degree: number of edges for that vertex • Undirected • Degree is the number of edges leaving vertex • Self loops count twice • Number of edges = 2*sum of vertex degrees • Directed • In-degree (incoming edge count) and out-degree (outgoing edge count) • Sum of all in-degrees (across all vertices) equals sum of all out-degrees
Traveling a graph • Path/walk/trail • Follow edges along vertices • Could repeat edge unless stated otherwise • Cycle/circuit/tour • Path that leads back to the original vertex • Usually assumes won’t follow same edge, but not always
Storing Graphs • 3 main methods • Edge List • Adjacency List • Adjacency Matrix
Storing graphs • Edge list • Keep a list of all edges • Store start, end, weight • Good if you need to work with all the edges at once • Kruskal’s MST algorithm • Not good at all if you want to know the adjacent edges for any one vertex
Edge List 6 1 2 Edge from 1 to 4, weight 8 5 3 8 Edge from 2 to 4, weight 5 3 4 9 Edge from 1 to 3, weight 3 Edge from 3 to 4, weight 9 Edge from 1 to 2, weight 6
Storing graphs • Adjacency list • Keep a list of edges in each vertex • Can be used for directed or undirected • Undirected – must keep consistent on both ends • Can keep weights per edge in the node list • Or, store pointer to a light edge structure • Good to find and iterate through edges for any vertex • Typical “default” implementation for a graph
Adjacency List 6 1 2 Node 1 Edge to 4 Weight 8 Edge to 3 Weight 3 Edge to 2 Weight 6 5 3 8 Node 2 Edge to 1 Weight 6 Edge to 4 Weight 5 3 4 9 Edge to 1 Weight 3 Edge to 4 Weight 9 Node 3 Node 4 Edge to 1 Weight 8 Edge to 2 Weight 5 Edge to 3 Weight 9
Storing graphs • Adjacency matrix • Store matrix indicating edges between vertices (rows/columns) • Directed: rows are from, columns are to • Value in the cell can be the weight of the edge • Bad if the graph is sparse, or too large • Can sometimes phrase graph calculations as matrix calculations this way; then, it can be more efficient to compute with
Adjacency Matrix 6 1 2 5 3 8 3 4 9
Breadth First/Depth First Search • Basic Overview: BFS keeps a QUEUE, DFS keeps a STACK • Get the next node (or, at beginning, starting node) • If node was already visited, then ignore it • Otherwise, mark it as visited, then for all adjacent vertices, put them on the queue/stack • Can store information like distance/time when a node is discovered • For DFS, can store time first found, and time when “finished”
Depth-First Search • Keep global list of visited T/F (or a number indicating which “tree” it is part of) • Recursive function: • Mark current node visited • For all adjacent edges: • If the node is unvisited, visit it • Basically, keep a stack of nodes that you want to visit • Forms the basis for several other operations
Breadth-first search • Keep a “distance” for each node, initialized to infinity • Keep queue of nodes to process, start with intro node • Repeatedly pop a node off the queue • Go through list of adjacent nodes • If node is infinity away, then set its distance to current distance plus one and add to queue • Has a few good uses • Shortest path in an undirected graph