leetcode 10. Regular Expression Matching edge cases

Please tell me how to deal with edge case such as s:"aa", p:"a*"in else if (p[j] == '*') branch. One way I know is to increase the size of dp like vector<vector<bool>> dp(s.size() + 1, vector<bool>(p.size() + 1, false) ); However, in that case the index of dp will not be the same with the index of for loop.

The problem is as follows:
Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.’ and ‘*’.

'.' Matches any single character. '*' Matches zero or more of the preceding element. 

The matching should cover the entire input string (not partial).

Here is the code:

class Solution { public:     bool isMatch(string s, string p) {         vector<vector<bool>> dp(s.size(), vector<bool>(p.size(), false) );         dp[0][0] = (s[0] == p[0] ? true : false);                  for (int i = 1; i < s.size(); i++) {             for (int j = 1; j < p.size(); j++) {                 if (s[i] == p[j] || p[j] == '.') {                     dp[i][j] = dp[i - 1][j - 1];                 } else if (p[j] == '*') {                     dp[i][j] = dp[i - 1][j] || dp[i][j - 2];                 } else if (s[i] != p[j]) {                     dp[i][j] = false;                 }             }         }                  return dp[s.size() - 1][p.size() - 1];     } }; 

Prove: If $T’$ is not a MST one of its edges may be replaced by a lighter edge in order to get a lighter spanning tree

Prove: If $ T’=(V,F’)$ is not a MST of $ G=(V,E)$ , then there are edges $ e’ \in F$ and $ e \in $ $ E$ \ $ F$ such that $ w(e)<w(e’)$ and $ T’$ \ $ \{e’\} \cup \{e\}$ is a spanning tree.

My idea:

Let $ T=(V,F)$ be a MST of $ G$

for all $ e \in T$ , try to insert it into $ T’$ and see if there is an edge in the cycle created that is heavier than $ e$ . If we found such $ e$ we are done.

If not, define $ G’=(V,F \cup F’)$

This graph obviously has a MST.

We didn’t find a suitable $ e$ in the previous part so all edges in $ F$ may be colored in red.

That means there is a MST $ T”=(V,F”)$ such that $ F” \ F’$ but that is impossible because $ T’$ is not a MST of $ G’$ , because $ T$ is lighter.

Does that seem correct?

When would you use an edge list graph data structure instead of an adjacency list or adjacency matrix?

In what applications would you choose an edge list over an adjacency list or an adjacency matrix?

Sample Question, VisuAlgo: Which best graph DS(es) should you use to store a simple undirected graph with 200 vertices, 19900 edges, and the edges need to be sorted? Suppose your computer only has enough memory to store 40000 entries.

There are three choices: adjacency lists, adjacency matrix, and an edge list.

Edge lists are the correct answer here because sorting by weight is most efficient, but what are some other use cases?


Find the probability of occurrence of each edge in a graph having $n$ nodes after $k$ operations

Given a graph with $ n$ nodes . The adjacency matrix of the graph is given. We do $ k$ operations on this graph. In each operation we choose two distinct nodes $ i$ and $ j$ (there are $ (n*(n-1))/2$ such choices) equiprobably (all choices are equiprobable) and if there exists a edge between $ i$ and $ j$ we delete that edge or else we draw an edge between the chosen pair of nodes.
We have to output a $ n*n$ matrix where the element at $ (i,j)th$ position denotes the probability of occurrence of edge connecting the nodes $ i$ and $ j$ in the final resulting graph.
The constraints on n and k are $ n<=50$ and $ k<=50$ .
I tried it using dynamic programming but could figure out transitions properly.
Can you please help me out.

Number of graphs that satisfies the property that edge weight is maximum of node values on which the edge is incident

I have an undirected weighted graph without multi edges. All the edge weights are whole numbers and known. I want to know in how many ways node values(node values are also whole numbers) can be assigned to the nodes such that the graph satisfies the condition that for every edge its edge weight is exactly equal to maximum of two node values this edge is incident on.

Minimum spanning tree with small set of possible edge weights

Given a undirected graph which only has two different edge weights $ x$ and $ y$ is it possible to make a faster algorithm than Prim’s algorithm or Kruskal’s algorithm?

I saw on Kruskal’s algorithm that it already assumes that the edges can be sorted in linear time with count sort, so then can we gain any benefit by knowing the weights of all of the edges ahead of time?

I don’t think there is any benefit, but I am not sure…

MST – adding an edge to graph

I have some difficulty proving the correctness of my solution to the following exercise. Let $ G = (V,E)$ undirected connected graph, $ w \colon E \to \mathbb{R}$ weight function. Let $ T$ a MST (minimum spanning tree) of $ G$ . Now, we add a new edge $ e’$ to $ E$ with weight $ w(e’)$ . Find an algorithm that updates $ T$ so it will be a MST of the new graph $ G’ = (V,E \cup \{e’\})$ . The time complexity of the algorithm should be $ O(V+E)$ .

Intuitively, the algorithm is quite simple: add $ e’$ to $ T$ , and then we got one cycle closed. Remove the edge with maximal weight from this cycle, and obtain a MST of $ G’$ .

However, I had some difficulties prove the correctness of this algorithm formally, and would appriciate some help.

BTW. I have read this post: If I have an MST, and I add any edge to create a cycle, will removing the heaviest edge from that cycle result in an MST? but unfortunately haven’t fully got the proof there.

Problem with making torus graph in Graph3D by identifying edge of a square graph

Background: I want to imply periodicity along the given edges for a graph. For example in a square lattice with identifying parallel edges, you can construct a torus. consider the following image

enter image description here

So I start with the construction of a square lattice network

nmax = 15;(*Length of lattice*) points = Flatten[Table[{i, j}, {i, -nmax, nmax}, {j, -nmax, nmax}],       1];(*list coordinate of the lattice*) d1 = (Sqrt[2] + 1)/2;(*Max distance to construct linked between coordination of the lattice*) d0 = 1/2;(*Min distance to construct linked between coordination of the lattice*) nn = Nearest[points -> "Index"]; (*function which determine the nearest of a vertex. we can do this*)  (*also by for example DistanceMatrixor or NearestNeighborGraph*) ha = Select[    Flatten[ParallelTable[Module[{pp}, pp = nn[points[[i]], {10, d1}];       Select[{i + 0 pp, pp,            Norm /@ ((points[[pp]]\[Transpose] -                 points[[i]])\[Transpose])}\[Transpose],          d1 > #[[3]] &][[All, {1, 2}]]], {i, 1, Length[points]}],      1], #[[1]] > #[[2]] &]; (*I use select to just consider one linke between two vortex ,*) (*This part is somehow hard to catch at a glince but it did not *) (*change following discussion. Consider this line  as a function*) (*making nearest neighbor links*) Graph3D[ha] 

where gives,

enter image description here

now I am looking to identifying edges. I use the following, for the left and right one

vortexL =points//SortBy[Flatten[Position[#[[All, 1]], Max[#[[All, 1]]]]], points[[#, 2]] &] &; vortexR =points//SortBy[Flatten[Position[#[[All, 1]], Min[#[[All, 1]]]]],points[[#, 2]] &] &; 

and for up and down edge we have

vortexU =points//SortBy[Flatten[Position[#[[All, 2]], Max[#[[All, 2]]]]], points[[#, 1]] &] &; vortexD =points//SortBy[Flatten[Position[#[[All, 2]], Min[#[[All, 2]]]]],points[[#, 1]] &] &; 

now i define identifier as

vchanger = {Table[vortexL[[i]] -> vortexR[[i]], {i, 1, Length@vortexL}],Table[vortexU[[i]]-> vortexD[[i]], {i, 1, Length@vortexU}]}; 

By applying it on ha (the link address) sequentially you can see how periodicity along those edges established,

ha = ha /. vchanger[[1]]; Graph3D[ha] 

enter image description here


ha = ha /. vchanger[[2]]; Graph3D[ha] 

where gives,

enter image description here

although it seems torus, by rotation it, you notify two crossings of the links

enter image description here

Question? So I am wondering, I did a mistake to construct the lattice and implication of periodic boundary condition, or this is the problem of Mathematica? Do someone has an option for Graph3D to make it correct shape?

Update My problem is almost the visualization of correct geometry this lattice has.